首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用System.Net.Mail通过gmail发送电子邮件

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

Stack Overflow用户
提问于 2011-01-13 14:02:44
回答 6查看 73.5K关注 0票数 57

我想通过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);

xxxx.com是谷歌应用程序中的邮件域名。谢谢..。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-01-13 14:20:55

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);
票数 82
EN

Stack Overflow用户

发布于 2011-01-25 22:50:54

我尝试使用上面的C#代码从Gmail向我的公司ID发送邮件。

在谷歌搜索的时候,我遇到了一个类似的code,它对我很有效。我在这里发布了这些代码。

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);
    }
}
票数 12
EN

Stack Overflow用户

发布于 2011-01-13 14:16:03

设置smtp.UseDefaultCredentials = false,使用smtp.Credentials =新密码(gMailAccount,NetworkCredential);

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4677258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档