.net 3.5 -如何在c#中使用SMTPclient发送电子邮件到gmail

本文关键字:SMTPclient 电子邮件 gmail net | 更新日期: 2023-09-27 17:50:21

我使用的是outlook 2003和visual studio 2008。我想开发一个应用程序,将电子邮件发送到任何领域。但我的代码失败,当我试图发送电子邮件到gmail, hotmail等实际上所有的消息都存储在C:'Inetpub'mailroot'Queue目录。请告诉我如何将邮件发送到gmail, hotmail。

Thanks in Advance

代码

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");

.net 3.5 -如何在c#中使用SMTPclient发送电子邮件到gmail

这是在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");

是正确的?这个方法有这样的重载吗?