主题:求助,C#中发送email失败,急求高手指点!
gbgd2003
[专家分:0] 发布于 2006-10-08 19:44:00
求高手救命啊。。!急死了。
源代码如下:
using System.Net.Mail;
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("gbgd2003@163.com");
mail.To.Add("gbgd2003@gmail.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
发送一直不成功,原来用System.Web.Mail也不行,请高手指点一下啊,错误信息在下面。
smtp肯定没问题,我用outlook express设置127.0.0.1发送邮件都成功了。
[img]http://img.album.pchome.net/00/05/13/24/ff6cb86d9eddb294032993db376e0b68.JPG[/img]
回复列表 (共3个回复)
沙发
cipc [专家分:2280] 发布于 2006-10-09 09:00:00
缺少了SMTP的验证了。
查看MSDN Credentials 属性
我个人还是比较喜欢JMAIL的,.net自带的类库不好用
板凳
gbgd2003 [专家分:0] 发布于 2006-10-09 10:18:00
楼上的,指点一下Jmail怎么玩啊?
3 楼
i2lingling [专家分:0] 发布于 2006-10-10 16:40:00
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.");
}
}
}
你看一下这个,是MSDN里.
你自己学一下SMTP协议,完了自己写一个类比.net的更好用.
我来回复