邮件发送代码
2010-05-14 10:57
302 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace 发送邮件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog();
openFileDialog1.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.Filter = "所有文件(*.*)|*.*";
if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
{
textBox3.Text=openFileDialog1.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
string address = "";
string displayName = "";
string w_txt收件人 =textBox3.Text.Trim();
if (w_txt收件人 == "")
{
MessageBox.Show("请输入收件人地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MailAddress from = new MailAddress(textBox5.Text, textBox7.Text); //邮件的发件人
MailMessage newMailMessage = new MailMessage();
string[] mailNames = (textBox1.Text).Split(',');
{
try
{
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
}
newMailMessage.From = from;
newMailMessage.To.Add(new MailAddress(address, displayName));
newMailMessage.Body = textBox4.Text;
newMailMessage.Subject = textBox2.Text;
//设置SMTP服务器地址
SmtpClient newclient = new SmtpClient("smtp.163.com");
newclient.UseDefaultCredentials = false;
//此处设置发件人邮箱的用户名和密码
newclient.Credentials = new System.Net.NetworkCredential(textBox5.Text, textBox6.Text); //发件人的账号和密码
newclient.DeliveryMethod = SmtpDeliveryMethod.Network;
newMailMessage.Attachments.Add(new Attachment(textBox3.Text)); // 发送附件
newMailMessage.Priority = MailPriority.High; //设置发送级别
//发送邮件
newclient.Send(newMailMessage);
}
MessageBox.Show("邮件发送完毕!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exp)
{
MessageBox.Show("邮件发送发生错误:" + exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripProgressBar1.Visible = false;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace 发送邮件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog();
openFileDialog1.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.Filter = "所有文件(*.*)|*.*";
if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
{
textBox3.Text=openFileDialog1.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
string address = "";
string displayName = "";
string w_txt收件人 =textBox3.Text.Trim();
if (w_txt收件人 == "")
{
MessageBox.Show("请输入收件人地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MailAddress from = new MailAddress(textBox5.Text, textBox7.Text); //邮件的发件人
MailMessage newMailMessage = new MailMessage();
string[] mailNames = (textBox1.Text).Split(',');
{
try
{
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
displayName = name.Substring(0, name.IndexOf('<'));
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
displayName = string.Empty;
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
}
newMailMessage.From = from;
newMailMessage.To.Add(new MailAddress(address, displayName));
newMailMessage.Body = textBox4.Text;
newMailMessage.Subject = textBox2.Text;
//设置SMTP服务器地址
SmtpClient newclient = new SmtpClient("smtp.163.com");
newclient.UseDefaultCredentials = false;
//此处设置发件人邮箱的用户名和密码
newclient.Credentials = new System.Net.NetworkCredential(textBox5.Text, textBox6.Text); //发件人的账号和密码
newclient.DeliveryMethod = SmtpDeliveryMethod.Network;
newMailMessage.Attachments.Add(new Attachment(textBox3.Text)); // 发送附件
newMailMessage.Priority = MailPriority.High; //设置发送级别
//发送邮件
newclient.Send(newMailMessage);
}
MessageBox.Show("邮件发送完毕!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exp)
{
MessageBox.Show("邮件发送发生错误:" + exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripProgressBar1.Visible = false;
}
}
相关文章推荐
- ASP.Net 2.0 发送邮件的代码
- asp发送邮件代码
- asp.net简单发送邮件代码
- 发送邮件的方式源代码可参考如下代码
- smtp发送带附件的邮件代码分享
- SharePoint 发送邮件之 Windows 2012 配置SMTP服务器及测试代码
- 通过SMTP发送邮件的Python代码
- 基于spring-boot的邮件发送的代码
- 使用phpmailer发送邮件,且需要激活完整代码
- 邮件发送代码(2)
- C# 发送邮件代码
- c#语言邮件发送参考代码
- asp.net jmail发送邮件代码
- Java的Spring框架中实现发送邮件功能的核心代码示例
- JAVA代码发送邮件示例和解释(二)
- iOS开发之邮件发送代码
- asp.net发送邮件代码
- 简单的发送邮件代码
- 发送短信/邮件/打电话的代码
- 简单的发送邮件代码