您的位置:首页 > 编程语言

邮件发送代码

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: