如何使用ASP.NET C发送电子邮件# [英] How To Send Email With ASP.NET C#
本文介绍了如何使用ASP.NET C发送电子邮件#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我是新学习C#,我想问如何通过C#发送电子邮件,请帮帮我:(我需要快点。
谢谢。
Hi I am new in learning C #, I want to ask how to send email through C #, Please Help Me :( , I need to quickly.
Thanks.
推荐答案
使用此方法
use this method
using System.Net.Mail;
namespace Common
{
public class SendEmail
{
public static void SendMessage(string subject, string messageBody, string toAddress, string ccAddress)
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
// Set the sender's address
message.From = new MailAddress("fromAddress");
// Allow multiple "To" addresses to be separated by a semi-colon
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))
{
message.To.Add(new MailAddress(addr));
}
}
// Allow multiple "Cc" addresses to be separated by a semi-colon
if (ccAddress.Trim().Length > 0)
{
foreach (string addr in ccAddress.Split(';'))
{
message.CC.Add(new MailAddress(addr));
}
}
// Set the subject and message body text
message.Subject = subject;
message.Body = messageBody;
// Set the SMTP server to be used to send the message
client.Host = "YourMailServer";
// Send the e-mail message
client.Send(message);
}
}
}
请浏览以下链接,他们已经给出了在asp中发送电子邮件的详细说明.net。
在ASP.Net 2.0中发送电子邮件 - 反馈表格 [ ^ ]
使用ASP.NET和C#发送邮件/联系表单 [ ^ ]
http:/ /www.dotnetcurry.com/ShowArticle.aspx?ID=65 [ ^ ]
如何发送简单的电子邮件与Asp-Net [ ^ ]
Please go through the following links, They have given detailed explanation for sending email in asp.net.
Send Email in ASP.Net 2.0 - Feed back Form[^]
Send Mail / Contact Form using ASP.NET and C#[^]
http://www.dotnetcurry.com/ShowArticle.aspx?ID=65[^]
How-To-Send-A-Simple-Email-With-Asp-Net[^]
请使用下面的代码是MailClass.cs,并尝试使用代码中的相同功能:(你想要SendMail())
Please use the code below which is a MailClass.cs and try using the functions in the same within your code behind:(where you want to SendMail())
using System;
using System.Net.Mail;
//using System.Web.Mail;
/// <summary>
/// Summary description for commonClass
/// </summary>
public class mailClass
{
#region Declaration
string retMsg = "";
private string m_toEmail;
private string m_fromEmail;
private string m_displayName;
private string m_subject;
private string m_body;
private string m_bcc;
private Attachment m_attachment;
const string c_success = "success";
const string c_error = "error";
#endregion
public mailClass()
{
//
// TODO: Add constructor logic here
//
}
#region Properties
public string vToEmail
{
get
{
return m_toEmail;
}
set
{
if (value != m_toEmail)
{
m_toEmail = value;
}
}
}
public string vFromEmail
{
get
{
return m_fromEmail;
}
set
{
if (value != m_fromEmail)
{
m_fromEmail = value;
}
}
}
public string vDisplayName
{
get
{
return m_displayName;
}
set
{
if (value != m_displayName)
{
m_displayName = value;
}
}
}
public string vSubject
{
get
{
return m_subject;
}
set
{
if (value != m_subject)
{
m_subject = value;
}
}
}
public string vBody
{
get
{
return m_body;
}
set
{
if (value != m_body)
{
m_body = value;
}
}
}
public Attachment vAttachment
{
get
{
return m_attachment;
}
set
{
if (value != m_attachment)
{
m_attachment = value;
}
}
}
public string vBcc
{
get
{
return m_bcc;
}
set
{
if (value != m_bcc)
{
m_bcc = value;
}
}
}
#endregion
#region Function
public string SendMail()
{
try
{
MailAddress from = new MailAddress(m_fromEmail, "World Class Medals");
MailMessage message = new MailMessage();
SmtpClient emailClient = new SmtpClient("smtp.appliedi.net");
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("", "");
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
if (m_toEmail.Contains(";"))
{
message = new MailMessage();
string[] addrCollection = m_toEmail.Split(";".ToCharArray());
string mailList = "";
foreach (string addr in addrCollection)
mailList = mailList + "<" + addr + "> ,";
mailList = mailList.TrimEnd(",".ToCharArray());
MailAddress to = new MailAddress(mailList);
message = new MailMessage(from, to);
}
else
{
MailAddress to = new MailAddress(m_toEmail);
message = new MailMessage(from, to);
}
if (m_bcc != null)
{
MailAddress bcc = new MailAddress(m_bcc);
message.Bcc.Add(bcc);
}
message.IsBodyHtml = true;
message.Subject = m_subject;
message.Body = m_body;
if (this.vAttachment != null)
message.Attachments.Add(this.vAttachment);
emailClient.Send(message);
retMsg = c_success;
}
catch (Exception ex)
{
retMsg = ex.Message.ToString();
}
return retMsg;
}
#endregion
}
这篇关于如何使用ASP.NET C发送电子邮件#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文