Send email from asp.net application

I configure all the settings for sending email using C #, but when I execute, I get the following error The requested address is not valid in its context. 74.125.53.109:25

my code

MailMessage mail = new MailMessage(); mail.To.Add(" to@gmail.com "); mail.From = new MailAddress(" from@gmail.com "); mail.Subject = "Test Email"; string Body = "<b>Welcome to CodeDigest.Com!!</b>"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = ConfigurationManager.AppSettings["SMTP"]; smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]); smtp.EnableSsl = true; smtp.Send(mail); 

Web.config

 <appSettings> <add key="SMTP" value="smtp.gmail.com"/> <add key="FROMEMAIL" value=" mail@gmail.com "/> <add key="FROMPWD" value="password"/> </appSettings> 
+4
source share
7 answers

Sending emails in .NET via Gmail This link contains the full code for sending emails and the correct operation of sending gmail emails on my computer.

+4
source

Port 25 is the default port, but not the correct port for sending email over SSL. Since you are using SSL, you need to set smtp.Port = 465 according to the Google help page on the topic: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

I think the address is not valid because it targets port 25 in the context of the SSL connection.

+1
source

I believe that I will simply post a collaborative message effort here:

Add this to your configuration file by changing the email address / username / password. You may need to change the port based on what Brian Rogers published.

  <system.net> <mailSettings> <smtp from=" some.user@gmail.com " deliveryMethod="Network"> <network host="smtp.gmail.com" port="587" enableSsl="true" userName=" some.user@gmail.com " password="mypassword"/> </smtp> </mailSettings> </system.net> 

Use this in your code

  MailMessage mail = new MailMessage(); mail.To.Add(" to@gmail.com "); mail.From = new MailAddress(" from@gmail.com "); mail.Subject = "Test Email"; string Body = "<b>Welcome to CodeDigest.Com!!</b>"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Send(mail); 
+1
source

You cannot specify port 25 from :25 after IP.

By default, port 25 will be installed, so you do not need it. If you want to change the port, use the following:

 mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpserverport", "portnumber" ); 
0
source

I have done this already, and I already checked it in Gmail.

fooobar.com/questions/1318623 / ...

Gmail Port = 465
Use SSL = true

0
source

This is a feature that works well for sending mail, I checked it and worked.

 private static bool testsendemail(MailMessage message) { try { MailMessage message1 = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); MailAddress fromAddress = new MailAddress(" FromMail@Test.com "); message1.From = fromAddress; message1.To.Add(" ToMail@Test1.com "); message1.Subject = "This is Test mail"; message1.IsBodyHtml = true; message1.Body ="You can write your body here" + message; // We use yahoo as our smtp client smtpClient.Host = "smtp.mail.yahoo.com"; smtpClient.Port = 587; smtpClient.EnableSsl = false; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential( " SenderMail@yahoo.com ", "YourPassword" ); smtpClient.Send(message1); } catch { return false; } return true; } 
0
source
 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.Net.Mail; public partial class SendMail : System.Web.UI.Page { protected void btnSend_Click(object sender, EventArgs e) { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress(" xxx@yourdomain.com "); msg.To.Add(txtTo.Text); //Text Box for To Address msg.Subject = txtSubject.Text; //Text Box for subject msg.IsBodyHtml = true; msg.Body = txtBody.Text; //Text Box for body msg.Priority = MailPriority.High; System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( "relay-hosting.secureserver.net", 25 ); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential( " xxx@yourdomain.com ", "yourpassword" ); client.Host = "relay-hosting.secureserver.net"; client.EnableSsl = false; object userstate = msg; client.Send(msg); } } 
0
source

All Articles