StringBuilder emailMessage = new StringBuilder();
emailMessage.Append("Dear Payment Team ,");
emailMessage.Append("<br><br>Please find the Payment instruction");
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress("[email protected]");
// Recipient e-mail address.
Msg.To.Add("[email protected]");
Msg.CC.Add("[email protected]");
Msg.Subject = "Timesheet Payment Instruction updated";
Msg.IsBodyHtml = true;
Msg.Body = emailMessage.ToString();
SmtpClient smtp = new SmtpClient();
//smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["HostName"];
smtp.Port = int.Parse(ConfigurationManager.AppSettings["PortNumber"]);
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail has been sent successfully.')</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
<appSettings>
<add key="HostName" value="The host name as given by my company" />
<add key="PortNumber" value="25" />
</appSettings>
I keep getting an exception tried changing the port number as specified but no success
System.Net.Mail.SmtpException was caught
Message=Failure sending mail.
Source=System
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at PAYMENT_DTALinesfollowup.Sendbtn_Click(Object sender, EventArgs e) in d:\AFSS-TFS\AFSS\Code\ERPNET\PAYMENT\DTALinesfollowup.aspx.cs:line 488
InnerException: System.Net.WebException
Message=Unable to connect to the remote server
Source=System
StackTrace:
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
InnerException: System.Net.Sockets.SocketException
Message=A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 125.63.68.148:25
Source=System
ErrorCode=10060
NativeErrorCode=10060
StackTrace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
InnerException:
Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.
Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is obsolete on some platforms and not recommended on others; for more information, see the Remarks section.
You need to give Username and password for smtp.
Use Below code :-
MailSettings.SMTPServer = Convert.ToString(ConfigurationManager.AppSettings["HostName"]);
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress("[email protected]");
// Recipient e-mail address.
Msg.To.Add("[email protected]");
Msg.CC.Add("[email protected]");
Msg.Subject = "Timesheet Payment Instruction updated";
Msg.IsBodyHtml = true;
Msg.Body = emailMessage.ToString();
NetworkCredential loginInfo = new NetworkCredential(Convert.ToString(ConfigurationManager.AppSettings["UserName"]), Convert.ToString(ConfigurationManager.AppSettings["Password"])); // password for connection smtp if u dont have have then pass blank
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Credentials = loginInfo;
//smtp.EnableSsl = true;
//No need for port
//smtp.Host = ConfigurationManager.AppSettings["HostName"];
//smtp.Port = int.Parse(ConfigurationManager.AppSettings["PortNumber"]);
smtp.Send(Msg);
First, you don't need to manually read the values in your .config file. You can set the in the System.Net section and your SmtpClient object will read them automatically:
<system.net>
<mailSettings>
<smtp from="Sender's display name <[email protected]>">
<network host="mailserver.yourdomain.com" port="25" userName="smtp_server_username" password="secret" defaultCredentials="false" />
</smtp>
</mailSettings>
</system.net>
Then, from your code, you just write:
SmtpClient smtp = new SmtpClient();
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(new MailAddress("[email protected]", "Recipient Display Name"));
mailMessage.Subject = "Some Subject";
mailMessage.Body = "One gorgeous body";
smtp.Send(mailMessage);
Coming back to your error, it would appear you have some kind of a network problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With