Send mail to users from gmail account using c#.net
Document by Alagappan -ganesanva@hotmail.com – + 919042710472
Click on File –> New Project in Visual Studio. Enter the solution name as MailUser.
Click OK
Right Click on the Project and add new item.
Add new form with name SendEmail.aspx
Put / Replace the below code in SendEmail.aspx
<!DOCTYPE html> <html xmlns=”http://www.w3.org/1999/xhtml”> <head runat=”server”> <title></title> </head> <body> <form id=”form1″ runat=”server”> <div> <table border=”0″ cellspacing=”2″ cellpadding=”2″> <tr> <td> Email To </td> <td> <asp:TextBox ID=”txtMailTo” runat=”server”></asp:TextBox> </td> </tr> <tr> <td> Subject </td> <td> <asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox> </td> </tr> <tr> <td> Body </td> <td> <asp:TextBox ID=”txtBody” runat=”server” TextMode=”MultiLine”></asp:TextBox> </td> </tr> <tr> <td> </td> <td> <asp:Button ID=”btnSend” runat=”server” Text=”Send” OnClick=”btnSend_Click” /> </td> </tr> </table> </div> </form> </body> </html> |
Put / Replace the below code In SendEmail.aspx.cs
public partial class SendEmail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSend_Click(object sender, EventArgs e) { string result=SendEmailtousers(txtMailTo.Text, txtSubject.Text, txtBody.Text); Response.Write(result); } protected string SendEmailtousers(string toAddress, string subject, string body) { string result = “Message Sent Successfully..!!”; string senderID = WebConfigurationManager.AppSettings[“SenderID”];// use sender’s email id here.. string senderPassword = WebConfigurationManager.AppSettings[“SenderPassword”]; // sender password hereā¦ try { SmtpClient smtp = new SmtpClient { Host = “smtp.gmail.com”, Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new System.Net.NetworkCredential(senderID, senderPassword), Timeout = 30000, }; MailMessage message = new MailMessage(senderID, toAddress, subject, body); message.CC.Add(WebConfigurationManager.AppSettings[“CCMail”]); smtp.Send(message); } catch (Exception ex) { result = “Error sending email.!!!””; } return result; } } |
Add the below code in web.config below Connectionstrings tag
<appSettings> <add key=”SenderID” value=”ganesvija@gmail.com”/> <add key=”SenderPassword” value=”*****”/> <add key=”CCMail” value=”ganesanva@hotmail.com”/> </appSettings> |
Replace **** with actual password in key SenderPassword.
Run the solution.
The Output as below,
GMAIL inbox,
Note:
In order to trigger mail from client, Enable this on the From Address Gmail account.
Turn on Allow for Less Secure apps.
https://www.google.com/settings/security/lesssecureapps
Reference:
https://support.google.com/accounts/answer/6010255?hl=en
Click below to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg7UPuVICB-batxhXag
Recent Posts
Categories
- All
- Angularjs training in Chennai
- ASP.NET Core
- dot net training
- dot net training in chennai
- dotnet full stack developer
- Free dotnet training
- information on dotnet
- Learn Java in chennai
- Learn Python at Karaikudi
- learn python online
- learn python online from chennai
- Linq Queries in .net
- mutual funds
- MVC Training Tutorials
- PHP Training in Chennai
- pmp training online
- power apps online training
- Python Training Online
- share market
- Sharepoint framework online training
- SharePoint Freelancers in Chennai
- software testing
- spfx online training
- Stock market
- Uncategorized