Suggested Videos
Part 75 - Logging exceptions to database
Part 76 - Customizing asp.net exception Logging
Part 77 - Sending emails using asp.net
In the previous session, we discussed about sending emails using asp.net. All the SMTP server settings were configured in code. In this session, we will discuss about specifying these settings in web.config file.
Web.config settings for SMTP server. All the attributes here are self explanatory.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" >
<network host="smtp.gmail.com" enableSsl="true" port="587"
userName="your_email@gmail.com" password="your_password"/>
</smtp>
</mailSettings>
</system.net>
Since the SMTP server settings are now configured in web.config. In code all you have to do is
1. Create an instance of the MailMessage class. Specify the FROM & TO email address, subject and Body
2. Create an instance of SmtpClient class, and send the email using Send() method. The SMTP settings will be automatically picked up from web.config file, when sending email.
SendEmail() method code
public static void SendEmail(string emailbody)
{
// Specify the from and to email address
MailMessage mailMessage = new MailMessage
("from_email@gmail.com", "to_email@gmail.com");
// Specify the email body
mailMessage.Body = emailbody;
// Specify the email Subject
mailMessage.Subject = "Exception";
// No need to specify the SMTP settings as these
// are already specified in web.config
SmtpClient smtpClient = new SmtpClient();
// Finall send the email message using Send() method
smtpClient.Send(mailMessage);
}
Part 75 - Logging exceptions to database
Part 76 - Customizing asp.net exception Logging
Part 77 - Sending emails using asp.net
In the previous session, we discussed about sending emails using asp.net. All the SMTP server settings were configured in code. In this session, we will discuss about specifying these settings in web.config file.
Web.config settings for SMTP server. All the attributes here are self explanatory.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" >
<network host="smtp.gmail.com" enableSsl="true" port="587"
userName="your_email@gmail.com" password="your_password"/>
</smtp>
</mailSettings>
</system.net>
Since the SMTP server settings are now configured in web.config. In code all you have to do is
1. Create an instance of the MailMessage class. Specify the FROM & TO email address, subject and Body
2. Create an instance of SmtpClient class, and send the email using Send() method. The SMTP settings will be automatically picked up from web.config file, when sending email.
SendEmail() method code
public static void SendEmail(string emailbody)
{
// Specify the from and to email address
MailMessage mailMessage = new MailMessage
("from_email@gmail.com", "to_email@gmail.com");
// Specify the email body
mailMessage.Body = emailbody;
// Specify the email Subject
mailMessage.Subject = "Exception";
// No need to specify the SMTP settings as these
// are already specified in web.config
SmtpClient smtpClient = new SmtpClient();
// Finall send the email message using Send() method
smtpClient.Send(mailMessage);
}
I am unable to configure my application to send emails using gmail. Please help..
ReplyDeleteBelow is the code:
Code:
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtemail.Text);
msg.To.Add(txtTo.Text);
msg.Subject = txtsubject.Text;
msg.Body = txtcomments.Text;
SmtpClient sm = new SmtpClient("smtp.gmail.com", 587);
sm.EnableSsl = true;
sm.DeliveryMethod = SmtpDeliveryMethod.Network;
sm.UseDefaultCredentials = false;
sm.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "XXXX");
sm.Send(msg);
Response.Write("Email sent");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Error: Failure sending mail
Dear sir,
ReplyDeleteBundles of thanks for such a help full material on .net
I have a problem in sending email exception occurred given below.
System.Net.Mail.SmtpException was unhandled by user code
Message=Failure sending mail.
Source=System
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at WebException.Logger.SendMail(String emailbody) in c:\users\numan\documents\visual studio 2010\Projects\WebException\WebException\Logger.cs:line 58
at WebException.WebForm1.Page_Load(Object sender, EventArgs e) in c:\users\numan\documents\visual studio 2010\Projects\WebException\WebException\WebForm1.aspx.cs:line 16
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
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=No connection could be made because the target machine actively refused it 66.102.1.109:587
Source=System
ErrorCode=10061
NativeErrorCode=10061
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:
I used this code for my blog on godaddy hostings. The code is working but mails are receiving in the spam box. How can i fix? Thanks in advance and my sincere thanks and appreciated for your work venkat. Your a great tutor, keep posting latest videos on asp.net
ReplyDeletegetting error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
ReplyDelete