通过Gmail发送使用System.Net.Mail电子邮件 [英] Send email using System.Net.Mail through gmail
本文介绍了通过Gmail发送使用System.Net.Mail电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过Gmail服务器发送电子邮件。我已经把下面的code,但在发送它被卡住。任何想法请......
MailMessage电子邮件=新MailMessage();
mail.From =新System.Net.Mail.MailAddress(apps@xxxx.com);
//创建smtpclient实例
SmtpClient SMTP =新SmtpClient();
smtp.Port = 465;
smtp.UseDefaultCredentials = TRUE;
smtp.Host =smtp.gmail.com;
smtp.EnableSsl = TRUE;
//收件人地址
mail.To.Add(新MailAddress(yyyy@xxxx.com));
//格式邮件正文
mail.IsBodyHtml = TRUE;
串ST =测试;
mail.Body = ST;
smtp.Send(邮件);
该xxxx.com是在谷歌企业应用套件邮件域。 谢谢...
解决方案
MailMessage电子邮件=新MailMessage();
mail.From =新System.Net.Mail.MailAddress(apps@xxxx.com);
//最重要的部分 - 配置SMTP客户端
SmtpClient SMTP =新SmtpClient();
smtp.Port = 587; // [1]你可以用465也试试,我一直用587,并获得成功
smtp.EnableSsl = TRUE;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2]新增本
smtp.UseDefaultCredentials = FALSE; // [3]改变了这
smtp.Credentials =新的NetworkCredential(mail.From,password_here); // [4]增加了这一点。注意,第一个参数是不是字符串。
smtp.Host =smtp.gmail.com;
//收件人地址
mail.To.Add(新MailAddress(yyyy@xxxx.com));
//格式邮件正文
mail.IsBodyHtml = TRUE;
串ST =测试;
mail.Body = ST;
smtp.Send(邮件);
I want to send a email through gmail server. I have put the following code but it is getting stuck while sending. Any idea please....
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);
The xxxx.com is a mail domain in Google apps. Thanks...
解决方案
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);
这篇关于通过Gmail发送使用System.Net.Mail电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文