0

Below is my code for sending mail and it shows an error

The server rejected the sender address. The server response was: 530 5.7.1 Authentication required

    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
    mail.To = "hsbanga@yahoo.com";
    mail.From = "hsbanga@yahoo.com";
    mail.Subject = "Query from agnihotrindt";

    mail.Body = "Name : " + TextBox1.Text + "<br/>" + "Email: " + TextBox2.Text + "<br/>Contact :" + TextBox3.Text + "<br/><br/><b>Address:</b><br/>" + TextBox4.Text + "<br/><b>Comment:</b><br/>" + TextBox5.Text ;
    mail.BodyFormat = System.Web.Mail.MailFormat.Html;

    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "hsbanga@yahoo.com";
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "secret";
    mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "false";

    System.Web.Mail.SmtpMail.SmtpServer = "smtp.mail.yahoo.com";
    System.Web.Mail.SmtpMail.Send(mail);
Marijke Buurlage
  • 331
  • 5
  • 21
  • 6
    I think the error is self explanatory. You need to add authenticate yourself against the mail server. – Joe Jan 15 '14 at 15:38
  • 1
    possible duplicate of [How can I send email from my local host using yahoo mail account?](http://stackoverflow.com/questions/12769270/how-can-i-send-email-from-my-local-host-using-yahoo-mail-account) – Joe Jan 15 '14 at 15:39
  • 1
    I removed the tags from your title, take a read of [this question](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) on the meta site to find out why – Joe Jan 15 '14 at 15:42

3 Answers3

1

Use :

SmtpClient emailClient = new SmtpClient("smtp.mail.yahoo.com");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("xyz@yahoo.com","*******"); 
emailClient.EnableSsl = true;
emailClient.Credentials = SMTPUserInfo;
emailClient.Port = 465;

MailMessage message = new System.Net.Mail.MailMessage("xyz@gmail.com", "someone@something.something", "fire!!", "Call up 911 and inform my house is on fire and my phone too");
emailClient.Send(message);
Community
  • 1
  • 1
1

Firstly, what version of .NET are you using? System.Web.Mail was deprecated at .NET 3.5 if I recall correctly. It is recommended to use System.Net.Mail namespace.

As for sending mail with credentials, I don't have C# code as an example as I add all my mail config to my web.config in the system.net element. Below is an example of how it should look for Yahoo.

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.mail.yahoo.com" port="465" userName="xyz@yahoo.com" password="*****" enableSsl="true" defaultCredentials="false" />
    </smtp>
  </mailSettings>
</system.net>

This also allows for changes to mail configuration without having to recompile your application every time a password changes. You can then instantiate your SmtpClient like this:

var client = new SmtpClient();
client.Send(mailMessage);
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
0

You can use this:

SmtpClient smtpClient = new SmtpClient("smtp.mail.yahoo.com", 465);
smtpClient.Credentials = new System.Net.NetworkCredential("hsbanga@yahoo.com", "secret");
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mail = new MailMessage();

smtpClient.Send(mail);
Ferit Buyukkececi
  • 2,665
  • 2
  • 20
  • 23