发邮件问题,请教大家 谢谢
我的邮件服务器是用 WinWebMail 来架设起来的
现在通过程序来发邮件
不知道是用 easymail 好呢 还是用jmail好
刚才我用easymail 去发 怎么都是失败 不知道为什么
大家 说说看,用什么好,发个代码看看 谢谢
[解决办法]
沙发,帮忙顶
下班了 ,明天再来看看
[解决办法]
用JMAIL发送方便
[解决办法]
System.Net.Mail
MailMessage类 与 SmtpClient类
[解决办法]
下面的代码示例演示如何以异步方式发送电子邮件。
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmptExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine( "[{0}] Send canceled. ", token);
}
if (e.Error != null)
{
Console.WriteLine( "[{0}] {1} ", token, e.Error.ToString());
} else
{
Console.WriteLine( "Message sent. ");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress( "jane@contoso.com ",
"Jane " + (char)0xD8+ " Clayton ",
System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress( "ben@contoso.com ");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190 ', '\u2191 ', '\u2192 ', '\u2193 '});
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1 " + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new
SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1 ";
client.SendAsync(message, userState);
Console.WriteLine( "Sending message... press c to cancel mail. Press any other key to exit. ");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn 't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith( "c ") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine( "Goodbye. ");
}
}
}
[解决办法]
经过测试没问题可以多发!
1.安装jmail4.3
2.找到jmail.dll(Program Files\Dimac\w3JMail4下)
3.执行Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe(可使用Visual Studio .Net 2003 命令提示),
格式如下:tlbimp c:\Program Files\Dimac\w3JMail4\jmail.dll /out:myJmail.dll /namespace:myJmail
就是我在Visual Studio .Net 2005命令提示下编译执行 tlbimp c:\Program Files\Dimac\w3JMail4\jmail.dll /out:myJmail.dll /namespace:myJmail
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Web.Util;
using myJmail;
using Tool;
using Manager;
using Entity;
public partial class UserControls_Jmaill : System.Web.UI.UserControl
{
string strCurrentPath = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
this.lblTitle.Text = "发送邮件控件 ";
myJmail.Message Jmail = new myJmail.Message();
DateTime t = DateTime.Now;
String Subject = this.txtTitle.Text;
String body = this.txtContent.Text;
string FromEmail = this.txtFormEmail.Text.Trim();//你的email
String ToEmail = this.txtToEmail.Text;//对方的email
String AddAttachment = this.FileUploadSubject.PostedFile.FileName;
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent = true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging = true;
//字符集,缺省为 "US-ASCII "
Jmail.Charset = "GB2312 ";
//信件的contentype. 缺省是 "text/plain ") : 字符串如果你以HTML格式发送邮件, 改为 "text/html "即可。
// Jmail.ContentType = "text/html ";
ToEmail = ToEmail.Replace( "\n ", " ").Replace( " ", " ");
string[] str = ToEmail.Split( ', ');
for (int i = 0; i < str.Length; i++)
{
//添加收件人
Jmail.AddRecipient(str[i], " ", " ");
Jmail.From = FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName = FromEmail;
//发件人邮件密码
Jmail.MailServerPassWord = " ";//FromEmail邮箱的登陆密码
//设置邮件标题
Jmail.Subject = Subject;
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg ",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType= "text/html ";删掉。否则会在邮件里出现乱码。
Jmail.AddAttachment(AddAttachment, true, null);
//邮件内容
Jmail.Body = body + t.ToString();
//加密文件
//Jmail.PGPEncrypt = true;
//Jmail发送的方法
Jmail.Send( "smtp.163.com ", false);
Jmail.ClearAttachments();
Jmail.ClearRecipients();
}
Jmail.Close();
}
catch (Exception ex)
{
this.lblMessage.Text = ex.Message;
}
}
}