日期:2014-05-17  浏览次数:20786 次

使用.Net自带的邮件类发送邮件报异常
本帖最后由 bobo3358 于 2013-10-21 14:00:36 编辑
写了一个发送邮件的小程序,之前 一直都可以发送邮件的,今天突然不能发送了,调试了一下发现报异常: InnerException:调用的目标发生异常,求助各位大侠;

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

namespace WindowsFormsApplication1
{
    public class Mail
    {
        public static bool Send(string to, string title, string content, bool MailServerBodyHtml)
        {           
                try
                {
                    System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
                    message.To = to;
                    message.From = "1197312019@qq.com";
                    message.Subject = title;
                    message.BodyFormat = System.Web.Mail.MailFormat.Html;
                    message.BodyEncoding = Encoding.UTF8;
                    message.Body = content;
                    message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
                    message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "********@qq.com"); //set your username here 
                    message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*******"); //set your password here 
                    //if (Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"].ToString()) != 25)
                    //{
                    message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//465
                    message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");