• MailMessage邮箱发送


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Mail;

    public class SendMailMessage
    {
    /// <summary>
    /// 发送邮箱
    /// </summary>
    /// <param name="toMail">目的邮件地址</param>
    /// <param name="title">发送邮件的标题</param>
    /// <param name="content">发送邮件的内容</param>
    /// <returns></returns>
    public string SendMail(string toMail, string title, string content)
    {
    string fromMail = "你的邮箱@qq.com";
    string fromPassword = "密码";
    System.Net.Mail.SmtpClient client = new SmtpClient();
    client.Host = "smtp.qq.com";
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential(fromMail, fromPassword);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;

    System.Net.Mail.MailMessage message = new MailMessage(fromMail, toMail);
    message.Subject = title;
    message.Body = content;
    message.SubjectEncoding = Encoding.UTF8;
    message.BodyEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;
    ////添加附件
    //Attachment data = new Attachment(@"附件地址如:f:\QQ2011.exe", System.Net.Mime.MediaTypeNames.Application.Octet);
    //message.Attachments.Add(data);
    //client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
    //client.SendAsync(mailMessage, mailMessage.To); //异步发送 (异步发送时页面上要加上Async="true" )
    try
    {
    client.Send(message);
    return "发送邮箱成功!";
    }
    catch (Exception ex)
    {
    return "发送邮箱失败!";
    }
    }
    void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

    if (e.Cancelled)
    {
    System.Web.HttpContext.Current.Response.Write("<script>alert('发送邮箱已被取消!');</script>");
    }
    if (e.Error == null)
    {
    System.Web.HttpContext.Current.Response.Write("<script>alert('发送邮箱成功!');</script>");
    }
    else
    {
    System.Web.HttpContext.Current.Response.Write("<script>alert('发送邮箱失败,错误信息:" + e.Error.Message + "');</script>");
    }

    }


    }
     
     
     
  • 相关阅读:
    Red5/FMS视频直播带宽计算
    基于NPOI开源框架写的ExcelHelper
    Using C# 4.0 and dynamic to parse JSON
    跟我学MVVM模式开发
    supermap使用代码示例(GIS)
    使用OpenXML将Excel内容读取到DataTable中
    ADO 数据类型转换表
    I don't like Regex...
    将Datatable转Excel少于4笔时汉字乱码4/26
    记录宝宝成长脚印3/31
  • 原文地址:https://www.cnblogs.com/liyuxin/p/2434840.html
Copyright © 2020-2023  润新知