如何在ASP.NET 3.5中发送电子邮件 [英] How to send email in ASP.NET 3.5
问题描述
我想在忘记密码的情况下为用户发送邮件代码。我希望当用户请求他的密码时,他的密码会自动发送到他的电子邮件ID,从大多数网站获取数据发生了。但我也无法发送简单的邮件。我的代码是:
Hi,I want to do code for sending mail to the user in case of Forget Password.i want that when user request for his password his password automatically send to his email id taking data from database as in most of the sites its happened.But i am unable to send even a simple mail also.My code is:
SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();
smtpclient.Host = "localhost";
try
{
MailAddress SendFrom = new MailAddress(T1.Text);
MailAddress SendTo = new MailAddress(T2.Text);
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
MyMessage.Subject = T3.Text;
MyMessage.Body = T4.Text;
T5.Text = "Message Sent";
catch (Exception ex)
{
T5.Text = ex.ToString();
}
在我的web.config中,我写这个来设置smtp服务器:
In my web.config,i am writing this to set smtp server:
<system.net>
<mailsettings>
<smtp deliverymethod="Network">
<network host="localhost" port="25" defaultcredentials="true" />
</smtp>
</mailsettings>
</system.net>
它成功运行但不发送邮件到我的邮箱ID在real.whats错了this.what我必须做更多。在我的系统IIS没有安装,它是否会产生问题?请立即gest。
Its running successfully but its not sending mail to my mail id in real.whats wrong with this.what i have to do more.In my system IIS is not installed,does it create the problem?please suggest.
推荐答案
只需使用此方法发送邮件即可。 (是的,您可能需要设置SMTP服务器,以便它可以从localhost PC发送发送邮件)。另外,不要忘记使用System.web.mail命名空间来使用此代码。这将做你需要的。
Simply use this method to send the mail. (yes you may need to setup your SMTP server so that it can send send mails from the localhost PC). Also, dont forget to use "System.web.mail" namespace for using this code. This will do the bit you need.
protected void btnSendMail_Click(object sender, EventArgs e)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = "xyz@gmail.com";
mailMessage.To = "abc@gmail.com";
mailMessage.Subject = "test";
mailMessage.BodyFormat = MailFormat.Text;
mailMessage.Body = "this is a test mail...";
mailMessage.Priority = MailPriority.High;
SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.Send(mailMessage);
lblStatus.Text = "Your mail was sent";
}
Anurag
@Cheers @
Anurag
@Cheers@
您必须设置localhost,使其成为连接到Internet的有效邮件服务器。
You have to set up localhost so that it is a valid mail server that is connected to the internet.
public class EmailUtility
{
public string FromEmailID { get; set; }
public string FromName { get; set; }
public string ToEmailID { get; set; }
public string CcEmailID { get; set; }
public string BccEmailID { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public bool IsBodyHtml { get; set; }
public List<String> AttachmentFiles { get; set; }
public string Error { get; set; }
public bool SendMail()
{
MailMessage mail = new MailMessage();
try
{
if (!String.IsNullOrEmpty(FromEmailID))
mail.From = new MailAddress(FromEmailID, FromName);
else
{
Error = "The Sender Address can't be empty.";
return false;
}
if (!String.IsNullOrEmpty(ToEmailID))
if (ToEmailID.Contains(";"))
{
string[] str = ToEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i] != String.Empty)
mail.To.Add(str[i]);
}
else
mail.To.Add(ToEmailID);
else
{
Error = "Recepient address can't be empty.";
return false;
}
if (!String.IsNullOrEmpty(BccEmailID))
if (BccEmailID.Contains(";"))
{
string[] str = BccEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i].Trim() != String.Empty)
mail.Bcc.Add(str[i]);
}
else
mail.Bcc.Add(BccEmailID);
if (!String.IsNullOrEmpty(CcEmailID))
if (CcEmailID.Contains(";"))
{
string[] str = CcEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i].Trim() != String.Empty)
mail.CC.Add(str[i]);
}
else
mail.CC.Add(CcEmailID);
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = IsBodyHtml;
mail.Priority = MailPriority.Normal;
if (AttachmentFiles != null)
foreach (string attachment in AttachmentFiles)
mail.Attachments.Add(new Attachment(attachment, MediaTypeNames.Application.Octet));
SmtpClient client = new SmtpClient();
//client.EnableSsl = true;
client.Send(mail);
mail.Dispose();
if (AttachmentFiles != null && AttachmentFiles.Count > 0)
AttachmentFiles.Clear();
return true;
}
catch (Exception ex)
{
Error = ex.ToString();
mail.Dispose();
return false;
}
}
_______________________________________________________________________
使用此功能
_______________________________________________________________________
_______________________________________________________________________
Use this Function also
_______________________________________________________________________
public static EmailUtility SendActivationEmail(User user)
{
EmailUtility utility = new EmailUtility();
utility.FromEmailID = Utils.EmailNotification;
utility.FromName = Utils.EmailFromName;
utility.ToEmailID = user.Email;
utility.Subject = "Shades n Style - New Account Activation";
StringBuilder mailBody = new StringBuilder();
mailBody.Append("Thank you for registering with <br/>");
mailBody.AppendLine("Username: " + user .Email+ "<br/>");
mailBody.AppendLine("Password: " + user.Password + "<br/><br/>");
//mailBody.AppendLine("To complete the registration process, please click on the following link:<br/>");
//mailBody.AppendLine(string.Format("<a href='{0}/Activate.aspx?Code={1}'>{0}/Activate.aspx?Account={1}</a> <br/><br/>", Utils.DomainName, Utils.EncryptText(user.Email)));
mailBody.AppendLine("This message was sent from an unmonitored email address. To contact us you can use the following page: <br/>");
mailBody.AppendLine(string.Format("<a href='{0}/contactus.aspx'>{0}/contactus.aspx</a> <br/><br/>", Utils.DomainName));
mailBody.AppendLine("The Shades n Style Team <br/>");
mailBody.AppendLine(string.Format("<a href='{0}'>{0}</a>", Utils.DomainName));
utility.Body = mailBody.ToString();
utility.IsBodyHtml = true;
utility.SendMail();
return utility;
}
这篇关于如何在ASP.NET 3.5中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!