c# winform,我写了个发送email的小程序,但是每次做完send之后,它不发,要关闭程序后才发,怎么回事?
如题:
我的代码:
string server = "aaa.bb.ccc ";
string htmlBody = " <b> Embedded image file. </b> <DIV> </DIV> ";
htmlBody += @ " <img alt= " " " " hspace=0 src= " "cid:112233 " " align=baseline border=0 > ";
htmlBody += " <DIV> </DIV> <b> This is the end of Mail... </b> ";
try
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html ");
MailMessage message = new MailMessage( "aaa@bbb.com ", "ccc@bbb.com ");
message.Subject = "5 ";
LinkedResource imageView = new LinkedResource( "c:\\pix\\a.jpg ", "image/jpeg ");
imageView.ContentId = "112233 ";
imageView.TransferEncoding = TransferEncoding.Base64;
htmlView.LinkedResources.Add(imageView);
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString( "plain text ", null, "text/plain "));
message.AlternateViews.Add(htmlView);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
message.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
运行到client.send(message)后,email就是没有发出来,似乎存在queue里面.一定要关闭程序,才会发送.这个怎么解决?
谢谢
[解决办法]
防火墙!!
[解决办法]
我这有个邮件发送的类
应该比自带的要好得多
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace SEMail
{
class csEmail
{
private string enter = "\r\n ";
/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为 " "
/// </summary>
public string Charset = "GB2312 ";
/// <summary>
/// 发件人地址
/// </summary>
public string From = " ";
/// <summary>
/// 发件人姓名
/// </summary>
public string FromName = " ";
/// <summary>
/// 回复邮件地址
/// </summary>
//public string ReplyTo= " ";
/// <summary>
/// 收件人姓名
/// </summary>
public string RecipientName = " ";
/// <summary>
/// 收件人列表
/// </summary>
private Hashtable Recipient = new Hashtable();
/// <summary>
/// 邮件服务器域名
/// </summary>
private string mailserver = " ";
/// <summary>
/// 邮件服务器端口号
/// </summary>
private int mailserverport = 25;
/// <summary>
/// SMTP认证时使用的用户名
/// </summary>
private string username = " ";
/// <summary>
/// SMTP认证时使用的密码
/// </summary>
private string password = " ";
/// <summary>
/// 是否需要SMTP验证
/// </summary>
private bool ESmtp = false;
/// <summary>
/// 是否Html邮件
/// </summary>
public bool Html = false;
/// <summary>
/// 邮件附件列表
/// </summary>
private System.Collections.ArrayList Attachments;
/// <summary>
/// 邮件发送优先级,可设置为 "High ", "Normal ", "Low "或 "1 ", "3 ", "5 "
/// </summary>
private string priority = "Normal ";
/// <summary>
/// 邮件主题
/// </summary>
public string Subject = " ";
/// <summary>
/// 邮件正文
/// </summary>
public string Body = " ";
/// <summary>
/// 收件人数量
/// </summary>
private int RecipientNum = 0;
/// <summary>
/// 最多收件人数量
/// </summary>
private int recipientmaxnum = 1;
/// <summary>
/// 密件收件人数量
/// </summary>
//private int RecipientBCCNum=0;
/// <summary>
/// 错误消息反馈
/// </summary>
private string errmsg;
/// <summary>
/// TcpClient对象,用于连接服务器
/// </summary>
private TcpClient tc;
/// <summary>
/// NetworkStream对象
/// </summary>
private NetworkStream ns;
/// <summary>
/// SMTP错误代码哈希表
/// </summary>
private Hashtable ErrCodeHT = new Hashtable();
/// <summary>
/// SMTP正确代码哈希表
/// </summary>
private Hashtable RightCodeHT = new Hashtable();
public csEmail()
{
Attachments = new System.Collections.ArrayList();
}
/// <summary>
/// 邮件服务器域名和验证信息
/// 形如: "user:paswww.server.com:25 ",也可省略次要信息。如 "user:paswww.server.com "www.server.com "
/// </summary>
public string MailDomain
{
set
{
string maidomain = value.Trim();
int tempint;
if (maidomain != " ")
{
tempint = maidomain.IndexOf( "@ ");
if (tempint != -1)
{
string str = maidomain.Substring(0, tempint);
MailServerUserName = str.Substring(0, str.IndexOf( ": "));
MailServerPassWord = str.Substring(str.IndexOf( ": ") + 1, str.Length - str.IndexOf( ": ") - 1);
maidomain = maidomain.Substring(tempint + 1, maidomain.Length - tempint - 1);
}
tempint = maidomain.IndexOf( ": ");
if (tempint != -1)
{
mailserver = maidomain.Substring(0, tempint);
mailserverport = System.Convert.ToInt32(maidomain.Substring(tempint + 1, maidomain.Length - tempint - 1));
}
else
{
mailserver = maidomain;
}
}
}
}