zoukankan      html  css  js  c++  java
  • 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>");
    }

    }


    }
     
     
     
  • 相关阅读:
    乱七八糟
    堆-heap
    转linux文件的读写
    @转EXT2->EXT3->EXT4
    (转)僵死进程与孤儿进程
    java
    poj-1062-昂贵的聘礼
    java 之 wait, notify, park, unpark , synchronized, Condition
    事物(笔记)
    BPX-tree
  • 原文地址:https://www.cnblogs.com/liyuxin/p/2434840.html
Copyright © 2011-2022 走看看