简体   繁体   English

如何使用C#中的SMTPclient向gmail发送电子邮件?

[英]How to send email to gmail using SMTPclient in C#?

I am using outloook 2003 and visual studio 2008. i want to develop an application that will send the email to any domain. 我正在使用outloook 2003和visual studio 2008.我想开发一个将电子邮件发送到任何域的应用程序。 but my code fails when i'm trying to send email to gmail, hotmail etc. actually all the messages is stored in C:\\Inetpub\\mailroot\\Queue directory. 但是当我尝试向gmail,hotmail等发送电子邮件时,我的代码失败了。实际上所有邮件都存储在C:\\Inetpub\\mailroot\\Queue目录中。 Please help me how i send the email to gmail, hotmail a/c. 请帮我如何将电子邮件发送到gmail,hotmail a / c。

Thanks in Advance 提前致谢

Code is 代码是

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

This is a good sample for Sending E-Mail with Gmail in C# 这是使用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 

Are you sure 你确定吗

message.From = new System.Net.Mail.MailAddress("Sumit.Dhingra@niit.com");

is right? 是对的? Does this method have an overload like this? 这种方法有这样的过载吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM