Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Please help me how i send the email to gmail, hotmail a/c.

Thanks in Advance

Code is

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.To.Add("[email protected]");            
message.Subject = "This is sample mail";
message.From = new System.Net.Mail.MailAddress("[email protected]");
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");
like image 418
sumit Avatar asked Apr 18 '11 08:04

sumit


People also ask

How do I send my SMTP email to Gmail?

Set up the app or device with the Gmail SMTP serverOn your device or in the app, enter smtp.gmail.com as the server address. In the Port field, enter one of the following numbers: If you're using SSL, enter 465. If you're using TLS, enter 587.

What is SMTP client for Gmail?

The SMTP server for Gmail is a free SMTP server that anyone across the globe can use. It allows you to manage email transactions from your Gmail account via email clients or web applications. Email clients are user-end mail applications. Some of the most popular ones are Thunderbird, Outlook, and Mac Mail.

Can I use Gmail for SMTP relay?

If your organization uses Microsoft Exchange or another SMTP email server, you can set up SMTP relay to route outgoing mail through Google. Use SMTP relay service options to: Filter messages for spam and viruses before they reach external recipients. Apply email security and advanced Gmail settings to outgoing messages.

What is SmtpClient C#?

SMTP Client in C# and VB.NETThe Simple Mail Transfer Protocol (SMTP) is the only standard protocol for sending mail messages over the Internet. GemBox. Email enables you to work with the SMTP protocol in C# and VB.NET using an SmtpClient class.


1 Answers

This is a good sample for Sending E-Mail with Gmail in C#

string from = [email protected]; //Replace this with your own correct Gmail Address

string to = [email protected] //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("[email protected]");

is right? Does this method have an overload like this?

like image 52
Soner Gönül Avatar answered Nov 05 '22 13:11

Soner Gönül