通过gmail使用System.Net.Mail发送电子邮件

JCT*_*TLK 56 .net c# email

我想通过gmail服务器发送电子邮件.我已经输入了以下代码,但它在发送时卡住了.请问....

MailMessage mail = new MailMessage();

        mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com");

        //create instance of smtpclient
        SmtpClient smtp = new SmtpClient();
        smtp.Port = 465;
        smtp.UseDefaultCredentials = true;

        smtp.Host = "smtp.gmail.com";            

        smtp.EnableSsl = true;

        //recipient address
        mail.To.Add(new MailAddress("yyyy@xxxx.com"));

        //Formatted mail body
        mail.IsBodyHtml = true;
        string st = "Test";

        mail.Body = st;
        smtp.Send(mail);
Run Code Online (Sandbox Code Playgroud)

xxxx.com是Google应用中的邮件域.谢谢...

Sar*_*fan 79

MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("apps@xxxx.com");

// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential(mail.From,  "password_here");  // [4] Added this. Note, first parameter is NOT string.
smtp.Host = "smtp.gmail.com";            

//recipient address
mail.To.Add(new MailAddress("yyyy@xxxx.com"));

//Formatted mail body
mail.IsBodyHtml = true;
string st = "Test";

mail.Body = st;
smtp.Send(mail);
Run Code Online (Sandbox Code Playgroud)

  • 一些观察:1.它是"smtp.gmail.com",而不是"smtp.google.com".我盯着我的代码半小时,直到我意识到我正在使用后者.2.端口587似乎比465好很多,尽管我读过的关于该主题的其他内容都说使用了端口465. 3.如果你使用的是两步验证,你最好是(参见http ://www.codinghorror.com/blog/2012/04/make-your-email-hacker-proof.html)您必须为您的应用程序生成一个新密码. (2认同)
  • 仅供参考,因为我遇到了这个问题:将你的SmtpClient对象包含在`using`子句中.如果您要在循环中使用客户端,则必须执行此操作. (2认同)

Abr*_*mJP 13

我尝试使用上述C#代码将邮件从Gmail发送到我的公司ID.在执行应用程序时,控件无限期地停止在语句中smtp.Send(mail);

谷歌搜索时,我遇到了类似的代码,对我有用.我在这里发布该代码.

class GMail
{
    public void SendMail()
    {  
        string pGmailEmail = "fromAddress@gmail.com";
        string pGmailPassword = "GmailPassword";
        string pTo = "ToAddress"; //abc@domain.com
        string pSubject = "Test From Gmail";
        string pBody = "Body"; //Body
        MailFormat pFormat = MailFormat.Text; //Text Message
        string pAttachmentPath = string.Empty; //No Attachments

        System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                          "smtp.gmail.com");
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                          "465");
        myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                          "2");
        //sendusing: cdoSendUsingPort, value 2, for sending the message using 
        //the network.

        //smtpauthenticate: Specifies the mechanism used when authenticating 
        //to an SMTP 
        //service over the network. Possible values are:
        //- cdoAnonymous, value 0. Do not authenticate.
        //- cdoBasic, value 1. Use basic clear-text authentication. 
        //When using this option you have to provide the user name and password 
        //through the sendusername and sendpassword fields.
        //- cdoNTLM, value 2. The current process security context is used to 
        // authenticate with the service.
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        //Use 0 for anonymous
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendusername",
            pGmailEmail);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
             pGmailPassword);
        myMail.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
             "true");
        myMail.From = pGmailEmail;
        myMail.To = pTo;
        myMail.Subject = pSubject;
        myMail.BodyFormat = pFormat;
        myMail.Body = pBody;
        if (pAttachmentPath.Trim() != "")
        {
            MailAttachment MyAttachment =
                    new MailAttachment(pAttachmentPath);
            myMail.Attachments.Add(MyAttachment);
            myMail.Priority = System.Web.Mail.MailPriority.High;
        }

        SmtpMail.SmtpServer = "smtp.gmail.com:465";
        SmtpMail.Send(myMail);
    }
}
Run Code Online (Sandbox Code Playgroud)


Sao*_*Ali 7

设置smtp.UseDefaultCredentials = false并使用smtp.Credentials = new NetworkCredential(gMailAccount,password);