Button_Click上的我的代码是
Massage = "Name :" + txtname.Text + "\nEmail :" + txtemail.Text + "\nPhone :" + TextBox3.Text + "\nMassage :" + TextBox4.Text;
eMail.Mail(null, "contact us", "vermarajneesh344@gmail.com", null, null, "contact us", Massage, null, null);
,背后的代码是
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());
//"firsttime@sector-17.com")
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("admin1@yoursite.com")
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 = "amangargptl@gmail.com";
// // 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("admin4@yoursite.com"));
////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;
}
和web.config上的代码是
<appSettings>
<add key="bh" value="Travel Holidays"/>
<add key="FromEmailAddress" value="info@Travelholidays.com"/>
<add key="ToAddress" value=""/>
<add key="ToCC" value=""/>
<add key="HostName" value="smtp.gmail.com"/>
<add key="UserName" value="info@Travelholidays.com"/>
<add key="Password" value="password"/>
<add key="smtpClientPort" value="587"/>
<add key="ESSL" value="true"/>
</appSettings>
谢谢.. 非常感谢帮助
答案 0 :(得分:1)
在web.config中使用这些设置
<system.net>
<mailSettings>
<smtp from="sender@gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" enableSsl="true" userName="Sender@gmail.com" password="*******" />
</smtp>
</mailSettings>
</system.net>
在你的.aspx.cs中使用
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("sender@gmail.com");
email.Body = emailMessage.ToString();
email.IsBodyHtml = true;
//Send Email;
SmtpClient client = new SmtpClient();
client.Send(email);
就是这样。希望它有效