Setting to Made in Web.config or App.config to send mail
<system.net>
<mailSettings>
<smtp from="fromMailAddress@gmail.com">
<network host="smtp.gmail.com" port="587" userName="fromMailAddress@sulekha.net" password="password123"/>
</smtp>
</mailSettings>
</system.net>
Use the Below is the Class To send Email
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
namespace CommonMethods
{
class Email
{
#region
constructor
public Email()
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
_UserName = mailSettings.Smtp.Network.UserName;
_Password = mailSettings.Smtp.Network.Password;
_smtpmailserver = mailSettings.Smtp.Network.Host;
_portnumber = mailSettings.Smtp.Network.Port;
_fromAddress = mailSettings.Smtp.From;
}
#endregion
constructor
private string
_UserName = "";
private string
_Password = "";
private string
_smtpmailserver = "";
private string
_fromAddress = "";
private int
_portnumber = 25;
public void SendMail(string FileName, string
toAddress, bool isBodyHTML, string Body, string
Subject)
{
try
{
string[] toadd = toAddress.Split(',');
MailMessage objMessage = new MailMessage();
foreach (string
strToAddress in toadd)
{
objMessage.To.Add(new MailAddress(strToAddress));
}
Attachment ObjAttachment = null;
if (FileName != null
&& FileName != "")
{
string[] attPathName = FileName.Split('|');
FileName = FileName.Replace("..\\",
"");
foreach (string
strPathName in attPathName)
{
ObjAttachment = new Attachment(strPathName);
objMessage.Attachments.Add(ObjAttachment);
}
}
objMessage.From = new MailAddress(_fromAddress);
objMessage.Priority = MailPriority.High;
objMessage.Headers.Add("Read-Receipt-To",
_fromAddress);
objMessage.Headers.Add("Return-Path",
_fromAddress);
objMessage.Headers.Add("Disposition-Notification-To",
_fromAddress);
// objMessage.ReplyToList.Add(new
MailAddress(fromAddress));
objMessage.Subject = Subject;
objMessage.Body = Body;
objMessage.IsBodyHtml = true;
SmtpClient client = new SmtpClient(_smtpmailserver,
_portnumber);
System.Net.NetworkCredential
objNetworkCredential = new System.Net.NetworkCredential(this._UserName,
this._Password);
client.Credentials = objNetworkCredential;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(objMessage);
if (ObjAttachment != null)
ObjAttachment.Dispose();
objMessage.Dispose();
}
catch (Exception
ex)
{
}
}
}
}
Use the Below Format to send Email
CommonMethods.Email
objmail = new CommonMethods.Email();
//objmail.SendMail("attachment
file location if no attachment send null", "to email address", "is the mail html content
or not(true or false)",
"content goes here", "Subject goes here");
objmail.SendMail(null,
"cool@gmail.com", true, "<html><head>head</head><body>body
body</body></html>", "TEST
SUBJECT");
Explanation on asp send email :
The Web.Config Or App.Config Part for asp send email
- The <system.net> tag represents the setting for network classes.
- The <mailSettings> tag configures mail sending options.
- The <smtp> tag has the attribute "from" which must be configured with the from address to send email.
- The <network> tag has the configuration information of smtp mail server connecting details like smtp mail server address, smtp mail server port number, smtp mail server username, smtp mail server password
The C# or ASP code for sending Email with .net
- To send email using .net three objects are used they are MailMessage, NetworkCredential, SmtpClient.
- MailMessage object is a collection of Mail header,Mail body,is the mail html, Attachment,from email address,to email address and etc.
- NetworkCredentials object has the information about email username and email password.
- SmtpClient onject has the information about the mail server like mail server hostname and the port in which its listening. The mail message object is passed to the smtpClient object with Network Credentials to process sending email in with .net.
The Sending part for sending Email with .net
- To send email using .net call create the object of the class of Email and pass the parameters
Do leave comments if you have any doubts so i can help you if there is any problem in sending email in asp.net C#.
http://www.docstorus.com/viewer.aspx?code=34fd84cc-6f0d-468f-aa79-62ad38775da2
ReplyDelete