我想在(c/s)winfrom中实现发送email,但要满足以下要求:
1.要利用指定的服务器的iis中的smtp来发送。
2.不能用自己本机的web服务,也就是说:把本机的iis给关掉(比如有些机器没有装iis)再来发送。
3.还有解决解析邮件域名问题。请各位高手们给点例子或参考资料,谢谢......
1.要利用指定的服务器的iis中的smtp来发送。
2.不能用自己本机的web服务,也就是说:把本机的iis给关掉(比如有些机器没有装iis)再来发送。
3.还有解决解析邮件域名问题。请各位高手们给点例子或参考资料,谢谢......
解决方案 »
- MVC2.0编辑后保存问题
- 搜索 功能的技术
- 想做个windows服务,定时执行asp.net程序。
- 如何在页面加载时,在DropDownList绑定了数据源以后再手动添加一项,并选上新添加的这一项
- 请教一个datagrid绑定多个DataSet的问题
- 汉字问题,急!
- 以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机
- 谁有中国各省市的ip地址库,,给一份
- 为什么我的htmlbutton作为服务器控件运行后,不会执行服务器代码?无反应!
- 请问这大代码是否线程安全
- 如何设置iframe中的heml控件属性。document.all.ifm.document.all.DropDownList1....设置无效。
- 如何这只iframe中textbox的值或样式?
英文原版:http://www.ajiang.net/luntan/w3jmail4.pdf
翻译后的:http://www.ajiang.net/luntan/w3jmail4_cn.pdf
V3.7中文:http://www.ajiang.net/luntan/w3JMial37.pdf
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using CDO;namespace SendMail
{
/// <summary>
/// 这个实例演示通过两种方式发送电子邮件(第一种使用MailMessage对象)
/// (第二种使用了SmtpMail类)
/// 第三个实例演示了附件的发送方法
/// </summary>
public class SendingMail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox fromAddress;
protected System.Web.UI.WebControls.TextBox sendToAddress;
protected System.Web.UI.WebControls.TextBox messageSubject;
protected System.Web.UI.WebControls.TextBox fileNameToAttach;
protected System.Web.UI.WebControls.Button SendMessageNoMessageObj;
protected System.Web.UI.WebControls.Button SendMessageWithFile;
protected System.Web.UI.WebControls.TextBox SMTPServerName;
protected System.Web.UI.WebControls.Label statusLabel;
protected System.Web.UI.WebControls.TextBox messageBody;
protected System.Web.UI.WebControls.Button SendMessage;
private void Page_Load(object sender, System.EventArgs e)
{
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SendMessage.Click += new System.EventHandler(this.SendMessage_Click);
this.SendMessageNoMessageObj.Click += new System.EventHandler(this.SendMessageNoMessageObj_Click);
this.SendMessageWithFile.Click += new System.EventHandler(this.SendMessageWithFile_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
/// <summary>
/// 使用MailMessage对象
/// </summary>
private void SendMessage_Click(object sender, System.EventArgs e)
{
/// <summary>
/// 创建一个名字为MailMessage的对象</summary>
MailMessage myMail = new MailMessage();
/// <summary>
/// 设置发送者电子邮件</summary>
/// <value>
/// 邮件地址必须是可以可以被中继的mail服务器</value>
myMail.From= fromAddress.Text;
/// <summary>
/// 设置接收者电子邮件</summary>
/// <value>
/// 你想发送到的邮件地址.</value>
myMail.To=sendToAddress.Text;
/// <summary>
/// 设置电子邮件主题</summary>
/// <value>
/// 电子邮件主题</value>
myMail.Subject=messageSubject.Text;
/// <summary>
/// 设置电子邮件内容</summary>
/// <value>
/// 可以设置内容格式,可以是Text或者Html</value>
myMail.Body=messageBody.Text;
/// <summary>
/// 设置Smtp服务器</summary>
/// <value>
/// 服务器是一个发送服务器. 确认它能中继信息</value>
SmtpMail.SmtpServer=SMTPServerName.Text;
/// <summary>
/// Send发放用来把邮件发送到邮件列表</summary>
/// <value>
/// 法送邮件</value>
try
{
SmtpMail.Send (myMail);
}
catch(Exception ex)
{
SMTPServerName.Text=ex.ToString();
}
/// 更新发送状态信息
statusLabel.Text = "Mail has been sent";
} ///<summary>
/// 使用SmtpMail类法送邮件
/// 只用设置他的一些属性就可以
///</summary>
private void SendMessageNoMessageObj_Click(object sender, System.EventArgs e)
{
/// <summary>
/// 设置发送者电子邮件</summary>
/// <value>
/// 邮件地址必须是可以可以被中继的mail服务器</value>
string from = fromAddress.Text;
/// <summary>
/// 设置接收者电子邮件</summary>
/// <value>
/// 你想发送到的邮件地址.</value>
string to = sendToAddress.Text;
/// <summary>
/// 设置电子邮件主题</summary>
/// <value>
/// 电子邮件主题</value>
string subject = messageSubject.Text;
/// <summary>
/// 设置电子邮件内容</summary>
/// <value>
/// 可以设置内容格式,可以是Text或者Html</value>
string body = messageBody.Text;
/// <summary>
/// 设置Smtp服务器</summary>
/// <value>
/// 服务器是一个发送服务器. 确认它能中继信息</value>
SmtpMail.SmtpServer=SMTPServerName.Text;
/// <summary>
/// Send发放用来把邮件发送到邮件列表</summary>
/// <value>
/// 法送邮件</value>
SmtpMail.Send(from, to, subject, body);
/// 更新发送状态信息
statusLabel.Text = "Mail has been sent";
} /// <summary>
/// 这个实例使用MailMessage演示怎么样发送附件
/// </summary>
private void SendMessageWithFile_Click(object sender, System.EventArgs e)
{
if (fileNameToAttach.Text == "" )
{
statusLabel.Text = "File name is required to send an attachment";
return;
}
/// <summary>
/// 创建一个MailMessage对象</summary>
MailMessage mailWithAttachment = new MailMessage();
/// <summary>
/// 创建一个MailAttachment对象</summary>
/// <value>
/// 设置附件的内容</value>
MailAttachment myAttachment = new MailAttachment(fileNameToAttach.Text);
/// <summary>
/// 把附件加到邮件对象的附件集合中</summary>
/// <value>
/// 添加操作</value>
mailWithAttachment.Attachments.Add(myAttachment);
/// <summary>
/// 设置发送者电子邮件</summary>
/// <value>
/// 邮件地址必须是可以可以被中继的mail服务器</value>
mailWithAttachment.From=fromAddress.Text;
/// <summary>
/// 设置接收者电子邮件</summary>
/// <value>
/// 你想发送到的邮件地址.</value>
mailWithAttachment.To=sendToAddress.Text;
/// <summary>
/// 设置电子邮件内容</summary>
/// <value>
/// 可以设置内容格式,可以是Text或者Html</value>
mailWithAttachment.Body=messageBody.Text;
/// <summary>
/// 设置电子邮件主题</summary>
/// <value>
/// 电子邮件主题</value>
mailWithAttachment.Subject=messageSubject.Text;
/// <summary>
/// 设置Smtp服务器</summary>
/// <value>
/// 服务器是一个发送服务器. 确认它能中继信息</value>
SmtpMail.SmtpServer=SMTPServerName.Text;
/// <summary>
/// Send发放用来把邮件发送到邮件列表</summary>
/// <value>
/// 法送邮件</value>
SmtpMail.Send(mailWithAttachment);
/// 更新发送状态信息
statusLabel.Text = "Mail has been sent";
}
}
}
这样的原文件行没有?
翻译后的:http://www.ajiang.net/luntan/w3jmail4_cn.pdf
V3.7中文:http://www.ajiang.net/luntan/w3JMial37.pdf
Outlook.Application outObj=new Outlook.Application();
Outlook.MailItemClass Item=new Outlook.MailItemClass();
Item = outObj.CreateItem(0) Item.To = "[email protected] " Item.Subject = "hello" Item.Body = "hell" Item.Attachments.Add("C:\abc.txt") Item.Send()