![](/img/trans.png)
[英]C# SmtpClient class not able to send email using gmail
[英]How to send email to gmail using SMTPclient in C#?
我正在使用outloook 2003和visual studio 2008.我想開發一個將電子郵件發送到任何域的應用程序。 但是當我嘗試向gmail,hotmail等發送電子郵件時,我的代碼失敗了。實際上所有郵件都存儲在C:\\Inetpub\\mailroot\\Queue
目錄中。 請幫我如何將電子郵件發送到gmail,hotmail a / c。
提前致謝
代碼是
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("sumitdawar@hotmail.com");
message.To.Add("sumitdawar@gmail.com");
message.Subject = "This is sample mail";
message.From = new System.Net.Mail.MailAddress("Sumit.Dhingra@niit.com");
message.Body = "this is the message body";
System.Net.Mail.SmtpClient sss = new System.Net.Mail.SmtpClient("HO-KKJ-MAIL.in.niit.com");
sss.UseDefaultCredentials = false;
sss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sss.Credentials = new System.Net.NetworkCredential("Sumit.Dhingrar", "password","domain");
這是使用C#
在Gmail
中發送電子郵件的一個很好的示例
string from = me@gmail.com; //Replace this with your own correct Gmail Address
string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
client.Credentials = new System.Net.NetworkCredential(from, "Password");
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
try
{
client.Send(mail);
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage );
} // end try
你確定嗎
message.From = new System.Net.Mail.MailAddress("Sumit.Dhingra@niit.com");
是對的? 這種方法有這樣的過載嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.