zoukankan      html  css  js  c++  java
  • C# WinFrom 发送邮件

    C# WinFrom 发送邮件
    C# Winforms 发送邮件

    发送邮件时用到以下来个命名空间:

    using System.Net;

    using System.Net.Mail;

    发送邮件的发信人邮箱必须使用smtp协议的邮箱,例如163、126、sina 等邮箱(但是163、226的早期注册的用户使用了些协议,126邮箱从06年6六月份后的注册的邮箱都不可用,而sina邮箱里可以自己设置是否启用smtp协议)其它邮箱类同

    以126为例 smtp服务器为:smtp.126.com

    以下为程序截图:





    以下为全部代码:

    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;

    using System.Net.Mail;



    namespace Mail

    {

    public partial class mailone : Form

    {

    public mailone()

    {

    InitializeComponent();

    }



    private void button1_Click(object sender, EventArgs e)

    {

    if (fjr.Text.Length == 0 || sjr.Text.Length == 0)

    {

    MessageBox.Show("请填写Email地址", "填写不完整", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

    return;

    }

    try

    {

    YanZheng(fjr.Text, sjr.Text); //验证邮件格式

    }

    catch (EmailErrorException ex)

    {

    MessageBox.Show(ex.Message, "Email格式错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

    return;

    }



    string fjrtxt = fjr.Text;

    string mmtxt = mm.Text;

    string sjrtxt = sjr.Text;

    string zttxt = zt.Text;

    string fjtxt = fj.Text;

    string nrtxt = nr.Text;



    string[] fasong = fjrtxt.Split('@');

    string[] fs = fasong[1].Split('.');



    //发送

    SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com"); //设置邮件协议

    client.UseDefaultCredentials = false;//这一句得写前面

    client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器

    client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt); //通过用户名和密码 认证



    MailMessage mmsg = new MailMessage(new MailAddress(fjrtxt), new MailAddress(sjrtxt)); //发件人和收件人的邮箱地址

    mmsg.Subject = zttxt; //邮件主题

    mmsg.SubjectEncoding = Encoding.UTF8; //主题编码

    mmsg.Body = nrtxt; //邮件正文

    mmsg.BodyEncoding = Encoding.UTF8; //正文编码

    mmsg.IsBodyHtml = true; //设置为HTML格式

    mmsg.Priority = MailPriority.High; //优先级



    if (fj.Text.Trim() != "")

    {

    mmsg.Attachments.Add(new Attachment(fj.Text));//增加附件

    }

    try

    {

    client.Send(mmsg);

    MessageBox.Show("邮件已发成功");

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }



    private void button3_Click(object sender, EventArgs e)

    {

    if (openFileDialog1.ShowDialog() == DialogResult.OK)

    {

    fj.Text = openFileDialog1.FileName; //得到附件的地址

    }

    }



    private void button2_Click(object sender, EventArgs e)

    {

    this.Close();

    }



    private bool YanZheng(string fmail, string smail)

    {

    string[] subStrings = smail.Split('@');

    string[] subStrings1 = fmail.Split('@');

    if (subStrings.Length != 2 || subStrings1.Length != 2)

    {

    throw new EmailErrorException();

    }

    else

    {

    int index = subStrings[1].IndexOf(".");

    int index1 = subStrings1[1].IndexOf(".");

    if (index <= 0 || index1 <= 0)

    {

    throw new EmailErrorException();

    }



    if (subStrings[1][subStrings[1].Length - 1] == '.' || subStrings1[1][subStrings[1].Length - 1] == '.')

    {

    throw new EmailErrorException();

    }

    }



    return true;

    }

    }

    }



    //以下为程序类文件,用于验证填写邮箱地址是否正确

    using System;

    using System.Collections.Generic;

    using System.Text;



    namespace Mail

    {

    public class EmailErrorException : ApplicationException

    {

    public string _message;



    public EmailErrorException()

    : base()

    {

    _message = null;

    }

    public EmailErrorException(string message)

    : base()

    {

    _message = message.ToString();



    }

    public EmailErrorException(string message, Exception myNew)

    : base(message, myNew)

    {

    _message = message.ToString();

    }



    public override string Message

    {

    get

    {

    return "Email格式错误。";

    }

    }

    }

    }

  • 相关阅读:
    多重背包POJ1276不要求恰好装满 poj1014多重背包恰好装满
    哈理工1053完全背包
    求最小公倍数与最大公约数的函数
    Bus Pass ZOJ 2913 BFS 最大中取最小的
    POJ 3624 charm bracelet 01背包 不要求装满
    HavelHakimi定理(判断一个序列是否可图)
    z0j1008Gnome Tetravex
    ZOJ 1136 Multiple BFS 取模 POJ 1465
    01背包 擎天柱 恰好装满 zjut(浙江工业大学OJ) 1355
    zoj 2412 水田灌溉,求连通分支个数
  • 原文地址:https://www.cnblogs.com/blogpro/p/11459878.html
Copyright © 2011-2022 走看看