-5
A
回答
2
快速修复与示例代码,请参阅http://www.orcsweb.com/blog/brad/sending-email-from-asp-net-4-c-sample-code/
对于更深入,见http://www.asp.net/general/videos/how-do-i-use-aspnet-to-send-email-from-a-web-site
+0
第一个链接中的代码不安全。 MailMessage实现IDisposable,但代码不会以任何方式处理消息。使用IDisposable的地方应该包括在内。 –
1
您需要使用System.Net.Mail,和该SmtpClient MAILMESSAGE和对象,像这样:
using (MailMessage mail = new MailMessage("[email protected]", "[email protected]"))
{
//supply host name and mail port
SmtpClient client = new SmtpClient("YourHostName", 25);
mail.Subject = "Hello";
mail.Body = "Mail body";
mail.IsBodyHtml = true;
client.Send(mail);
}
这里的一类我创建的encompa sses以上:
/// <summary>
/// Wrapper class for the System.Net.Mail objects
/// </summary>
public class SmtpMailMessage : IDisposable
{
#region declarations
MailMessage Message;
SmtpClient SmtpMailClient;
#endregion
#region constructors
/// <summary>
/// Default constructor for the SmtpMailMessage class
/// </summary>
public SmtpMailMessage()
{
//initialize the mail message
Message = new MailMessage();
Message.Priority = MailPriority.Normal;
Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
Message.From = new MailAddress("[email protected]");
//initialize the smtp client
SmtpMailClient = new SmtpClient();
SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpMailClient.Host = "192.168.0.21";
SmtpMailClient.Port = 25;
}
/// <summary>
/// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
/// SMTP host and port number
/// </summary>
/// <param name="HostIP">The IP address of the exchange server</param>
/// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
public SmtpMailMessage(string HostIP, int PortNumber) : this()
{
//override the smtp host value
SmtpMailClient.Host = HostIP;
//override the smtp port value
SmtpMailClient.Port = PortNumber;
}
#endregion
#region subject/body
/// <summary>
/// The body content of the mail message
/// </summary>
public string Body
{
get
{
return Message.Body;
}
set
{
Message.Body = value;
}
}
/// <summary>
/// the subject of the mail message
/// </summary>
public string Subject
{
get
{
return Message.Subject;
}
set
{
Message.Subject = value;
}
}
#endregion
#region mail type
/// <summary>
/// Gets or sets a value that determines whether the mail message
/// should be formatted as HTML or text
/// </summary>
public bool IsHtmlMessage
{
get
{
return Message.IsBodyHtml;
}
set
{
Message.IsBodyHtml = value;
}
}
#endregion
#region sender
/// <summary>
/// Gets or sets the from address of this message
/// </summary>
public string From
{
get
{
return Message.From.Address;
}
set
{
Message.From = new MailAddress(value);
}
}
#endregion
#region recipients
/// <summary>
/// Gets the collection of recipients
/// </summary>
public MailAddressCollection To
{
get
{
return Message.To;
}
}
/// <summary>
/// Gets the collection of CC recipients
/// </summary>
public MailAddressCollection CC
{
get
{
return Message.CC;
}
}
/// <summary>
/// Gets the collection of Bcc recipients
/// </summary>
public MailAddressCollection Bcc
{
get
{
return Message.Bcc;
}
}
#endregion
#region delivery notification
/// <summary>
/// Gets or sets the delivery notification settings for this message
/// </summary>
public DeliveryNotificationOptions DeliveryNotifications
{
get
{
return Message.DeliveryNotificationOptions;
}
set
{
Message.DeliveryNotificationOptions = value;
}
}
#endregion
#region priority
/// <summary>
/// Gets or sets the Priority of this message
/// </summary>
public MailPriority PriorityLevel
{
get
{
return Message.Priority;
}
set
{
Message.Priority = value;
}
}
#endregion
#region send methods
/// <summary>
/// Sends the message anonymously (without credentials)
/// </summary>
public void Send()
{
SmtpMailClient.Send(Message);
}
/// <summary>
/// Sends the message with authorization from a network account
/// </summary>
/// <param name="Username">The Windows username of the authorizing user</param>
/// <param name="Password">The Windows password of the authorizing user</param>
/// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
public void Send(string userName, string password, string domain)
{
//attach a network credential to this message using the information passed into the method
SmtpMailClient.Credentials = new NetworkCredential(userName, password, domain);
//send the message
SmtpMailClient.Send(Message);
}
#endregion
#region IDisposable implementation
~SmtpMailMessage()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Message != null)
Message.Dispose();
Message = null;
SmtpMailClient = null;
}
}
#endregion
}
相关问题
- 1. 从localhost ASP.net发送电子邮件
- 2. 定期从Asp.Net发送电子邮件
- 3. 如何从ASP.NET的* any *电子邮件地址发送电子邮件?
- 4. 如何从PHPmyAdmin发送电子邮件?
- 5. 如何从Node.js发送电子邮件?
- 6. 如何从C发送电子邮件
- 7. 发送电子邮件从
- 8. ASP.NET MVC发送电子邮件附件
- 9. 如何在asp.net中通过电子邮件发送邮件
- 10. asp.net的MVC发送电子邮件错误:无法发送电子邮件
- 11. 在asp.net发送电子邮件发送密码而不是电子邮件
- 12. 如何从Asp.net Mvc-3发送电子邮件?
- 13. 你如何从ASP.NET发送大量电子邮件?
- 14. 发送电子邮件提醒与ASP.NET
- 15. 发送电子邮件asp.net c#
- 16. 使用ASP.NET发送电子邮件
- 17. 通过asp.net发送电子邮件3.5
- 18. 在asp.net中发送电子邮件
- 19. ASP.net 3.5发送网页电子邮件
- 20. 用ASP.Net发送海量电子邮件
- 21. 在asp.net发送大量电子邮件
- 22. PHP发送电子邮件多次发送电子邮件
- 23. 如何通过电子邮件发送电子邮件地址
- 24. 如何发送电子邮件到特定的电子邮件?
- 25. 如何从电子表格发送电子邮件
- 26. 你如何从Android的任何电子邮件地址发送电子邮件?
- 27. 从VBA电子邮件程序发送HTML电子邮件
- 28. 如何使用java邮件从android发送电子邮件
- 29. 如何发送电子邮件在Java
- 30. 如何发送UTF-8电子邮件?
我想你是问你是否需要一个电子邮件帐户从ASP.Net发送电子邮件?请澄清你的问题。 – Rumplin