Reputation: 15
My Code on Button_Click is
Massage = "Name :" + txtname.Text + "\nEmail :" + txtemail.Text + "\nPhone :" + TextBox3.Text + "\nMassage :" + TextBox4.Text;
eMail.Mail(null, "contact us", "[email protected]", null, null, "contact us", Massage, null, null);
and the code behind is
public class eMail
{
public static bool Mail(string FromEmailAddress, string FromDisplayName, string ToAddress, string ToAddressCC, string ToAddressBCC, string Subject, string bodytxt, string HostName, string AttachFileName)
{
bool functionReturnValue = false;
//*******example
//// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
//// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
if (string.IsNullOrEmpty(FromEmailAddress))
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["FromEmailAddress"].ToString()))
FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
MailAddress fromAddress = new MailAddress(FromEmailAddress, FromDisplayName);
// // You can specify the host name or ipaddress of your server
//// Default in IIS will be localhost
if (string.IsNullOrEmpty(HostName))
{
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["HostName"].ToString()))
HostName = "smtp.gmail.com"; //"mail.osmor.org";
else
HostName = ConfigurationManager.AppSettings["HostName"].ToString();
}
smtpClient.Host = HostName;
////Default port will be 25
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["smtpClientPort"].ToString()))
smtpClient.Port = 465;
else
smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpClientPort"].ToString());
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ESSL"].ToString()))
{
if (ConfigurationManager.AppSettings["ESSL"].ToString() == "true")
smtpClient.EnableSsl = true;
else
smtpClient.EnableSsl = false;
}
////From address will be given as a MailAddress Object
message.From = fromAddress;
//// To address collection of MailAddress
if (string.IsNullOrEmpty(ToAddress))
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ToAddress"].ToString()))
ToAddress = ConfigurationManager.AppSettings["ToAddress"].ToString();
string[] tmp = null;
int i = 0;
tmp = Mainclass.SplitByString(ToAddress, ",");
for (i = 0; i <= tmp.Length - 1; i++)
{
message.To.Add(tmp[i].ToString());
//"[email protected]")
message.Subject = Subject;
//"Feedback"
}
//// CC and BCC optional
//// MailAddressCollection class is used to send the email to various users
// // You can specify Address as new MailAddress("[email protected]")
if (string.IsNullOrEmpty(ToAddressCC))
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ToCC"].ToString()))
ToAddressCC = ConfigurationManager.AppSettings["ToCC"].ToString();
if (!string.IsNullOrEmpty(ToAddressCC))
{
tmp = Mainclass.SplitByString(ToAddressCC, ",");
for (i = 0; i <= tmp.Length - 1; i++)
{
message.CC.Add(tmp[i].ToString());
//
}
}
//ToAddressBCC = "[email protected]";
// // You can specify Address directly as string
if (!string.IsNullOrEmpty(ToAddressBCC))
{
tmp = Mainclass.SplitByString(ToAddressBCC, ",");
for (i = 0; i <= tmp.Length - 1; i++)
{
message.Bcc.Add(new MailAddress(tmp[i].ToString()));
}
}
// //message.Bcc.Add(new MailAddress("[email protected]"));
////Body can be Html or text format
// //Specify true if it is html message
message.IsBodyHtml = true;
String UserName, Password;
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["UserName"].ToString()))
UserName = ConfigurationManager.AppSettings["UserName"].ToString();
else
UserName = "UserName";
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Password"].ToString()))
Password = ConfigurationManager.AppSettings["Password"].ToString();
else
Password = "Password";
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
//// Message body content
message.Body = bodytxt;
if (!string.IsNullOrEmpty(AttachFileName))
message.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath("~/" + AttachFileName)));
// // Send SMTP mail
smtpClient.Send(message);
// lblStatus.Text = "Email successfully sent.";
functionReturnValue = true;
}
catch (Exception ex)
{
bodytxt = "Send Email Failed.<br>" + ex.Message;
//;
//Mainclass.WriteErorrFile(ex.Message, "Macadamia", "Mail");
//c.msg(ex.Message, response)
functionReturnValue = false;
// throw;
}
return functionReturnValue;
}
and code on web.config is
<appSettings>
<add key="bh" value="Travel Holidays"/>
<add key="FromEmailAddress" value="[email protected]"/>
<add key="ToAddress" value=""/>
<add key="ToCC" value=""/>
<add key="HostName" value="smtp.gmail.com"/>
<add key="UserName" value="[email protected]"/>
<add key="Password" value="password"/>
<add key="smtpClientPort" value="587"/>
<add key="ESSL" value="true"/>
</appSettings>
Thanks.. help would be greatly appreciated
Upvotes: 1
Views: 382
Reputation: 330
Use these setting in your web.config
<system.net>
<mailSettings>
<smtp from="[email protected] ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" enableSsl="true" userName="[email protected]" password="*******" />
</smtp>
</mailSettings>
</system.net>
In your .aspx.cs use
StringBuilder emailMessage = new StringBuilder();
emailMessage.Append("Email body here");
MailMessage email = new MailMessage();
email.To.Add(new MailAddress("ReceiverEmailAddress"));
email.Subject = "Mail Subject";
email.From = new MailAddress("[email protected]");
email.Body = emailMessage.ToString();
email.IsBodyHtml = true;
//Send Email;
SmtpClient client = new SmtpClient();
client.Send(email);
That's it. Hope it works
Upvotes: 1