1 using System;
2 using System.Web;
3 using System.Web.Services;
4 using System.Web.Services.Protocols;
5 using System.Web.Mail;
6
7 [WebService(Namespace = "http://tempuri.org/")]
8 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
9
10 public class Service : System.Web.Services.WebService
11 {
12 public Service()
13 {
14
15 //Uncomment the following line if using designed components
16 //InitializeComponent();
17 }
18
19 [WebMethod]
20 public string HelloWorld(String username, String body)
21 {
22 MailMessage mail = new MailMessage();
23 mail.To = "yujiatao416@163.com";
24 mail.From = "yujiatao416@163.com";
25 mail.BodyFormat = MailFormat.Text;
26 mail.Subject = username;
27 mail.Body = body;
28
29 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
30
31 //username为mail.From里面MMMM发送邮件的用户名
32 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "hbluojiahui");
33
34 //password为mail.From里面MMMM发送邮件的密码
35 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "ZycgwXncgw");
36
37 //smtp.163.com为163的邮件服务器地址
38
39 SmtpMail.SmtpServer = "smtp.163.com";
40 SmtpMail.Send(mail);
41 return " " + username + " " + "发送成功!";
42 }
43
我在测试HelloWorld()方法时出错。错误信息如下:
System.Web.HttpException: óê?t?T·¨·¢?íμ? SMTP ·t???÷?£′?ê?′í?ó′ú???a 0x80040217?£·t???÷?ìó|?a not available --->
System.Reflection.TargetInvocationException: 调用的目标发生了异常。
---> System.Runtime.InteropServices.COMException: óê?t?T·¨·¢?íμ? SMTP ·t???÷?£′?ê?′í?ó′ú???a 0x80040217?£·t???÷?ìó|?a not available
--- 内部异常堆栈跟踪的结尾 --- 在 System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
在 System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
在 System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
--- 内部异常堆栈跟踪的结尾 --- 在 System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
在 System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message) 在 System.Web.Mail.SmtpMail.Send(MailMessage message)
在 Service.HelloWorld(String username, String body) 位置 e:\MovieCtrl\App_Code\Service.cs:行号 40
备注:我是想在Flex中调用这个webservices,但是我测试这个方法就出错,哎,弄1天了,还没有解决,请高手帮忙,小弟不胜感激~!
有可能是用户名凭证的问题,应该要密码的,另外查一下163能不能给自己发邮件
下面我的代码示例:
//smtp服务器
SmtpClient smtpClient = new SmtpClient(smtpSetting.Server);
//邮件信息
MailMessage message = new MailMessage();
//客户端用户名验证与此处发件人要相同
message.From = new MailAddress(smtpSetting.Sender);
//收件人
foreach (string to in recipient)
{
message.To.Add(to);
}
//主体内容是否HTML
message.IsBodyHtml = isBodyHtml;
//编码方式
message.SubjectEncoding = encoding;
message.BodyEncoding = encoding;
//主题
message.Subject = subject;
//主体内容
message.Body = body;
//附件
message.Attachments.Clear();
if (files != null && files.Length != 0)
{
for (int i = 0; i < files.Length; ++i)
{
Attachment attach = new Attachment(files[i]);
attach.NameEncoding = Encoding.Default;
attach.ContentDisposition.Inline = false;
message.Attachments.Add(attach);
}
}
//用户名凭证
smtpClient.Credentials = new NetworkCredential(smtpSetting.Sender, smtpSetting.Password);
//发送方式:网络方式
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//客户端发送邮件
smtpClient.Send(message);