0

I am doing my project in mvc4 using c#. I have a contact page i my website. My need is that i have to receive messages to my email id from other id's, when clicking the Send button.I use the following code

public void ReceiveMail(string name,string email,string message)
{
    MailMessage msg = new MailMessage();
    HttpContext ctx = HttpContext.Current;
    msg.To.Add(new MailAddress("MyEmailId"));
    msg.From = new MailAddress(email);
    msg.Subject =name + "send a message";
    msg.Priority = MailPriority.High;
    msg.Body = message;
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");// i am confused what to write here
    SmtpServer.Send(msg);
}

It shows the error

 The SMTP server requires a secure connection or the client was not authenticated.
 The server response was: 5.7.0 Must issue a STARTTLS command first. 
 at4sm42219747pbc.30 - gsmtp

I don't know from which server i got the mail. Then how can i solve this issue . Please help me

neel
  • 4,583
  • 12
  • 37
  • 63

4 Answers4

1

Sending emails with Gmail requires some additional settings. At first, port number should be 587 (instead of default 25). At second, Gmail requires secure connection. And of course you should provide valid credentials.

All in all, initialization of SmtpClient should look like this:

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential("username@gmail.com", "password");
Andrei
  • 53,252
  • 9
  • 82
  • 104
1

as the error says, a STARTTLS command should be used first. Thas means gmail only accepts mail via secure connection. In this answer enableSsl was set to true. As the documentation from microsoft says, the SmtpClient class has such an property too. Furthermore you should leave your credentials in the smptClient. I think gmail only accepts mail from authenticate users. I think the whole problem is solved here.

Community
  • 1
  • 1
0xBADF00D
  • 890
  • 1
  • 11
  • 23
0

You need to use NetworkCredential to login into Gmail SMTP server. Error is very apparent.

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("your-email", "your-password");
Saeed Neamati
  • 33,583
  • 40
  • 128
  • 183
  • actually i want receive mails from other emails ..but using this i got the mail from my address – neel Nov 13 '13 at 07:30
0

Have you tried:

smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials = 
    new NetworkCredential("SenderGmailUserName", "SenderPassword");
Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Rohit
  • 9,362
  • 6
  • 43
  • 76
  • actually i want receive mails from other emails ..but using this i got the mail from my address – neel Nov 13 '13 at 07:32
  • @Parvathiiiii Please take a look at my updated answer.The sender can send mail using his credentials .You need to provide functionality to your user to provide there Username and Password (Not an recommended approach) – Rohit Nov 13 '13 at 07:39
  • That is not sure sender always send from his gmail account, and also i have to receive mails, then how i know the senders password – neel Nov 13 '13 at 07:50
  • 1
    well you can either make it mandatory to use gmail or you can use other smtp or even combine them in drop down list(like yahoo,AOL,etc..). You have to write seperate code for them. – Rohit Nov 13 '13 at 08:56