我有一个程序,它将生成PDF格式的财务报告,然后调用SMTP Web服务发送电子邮件。
请在下面找到我的插图代码:
using (SMTPServiceSoapClient client = new SMTPServiceSoapClient())
{
for (no. of customers)
{
try
{
//step 1: generate_report
generate_pdf();
//step 2: call SMTP web service and send email
client.SendEmail();
}
catch(Exception e)
{
}
}
}
第一封电子邮件总是成功发送。
第二封电子邮件将始终抛出异常(“请求通道在00:24:59.9969997之后等待回复时超时。增加传递给Request的调用的超时值或增加Binding上的SendTimeout值。分配给此操作的时间可能是较长超时的一部分。“)
而且,成功/失败模式就像
成功
失败
成功
失败
...
即使我设置了很长的(例如2小时)SendTimeOut值。程序只会等待几秒钟然后抛出异常。
以下是我的app.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SMTP ServiceSoap" sendTimeout="00:25:00" maxBufferSize="536870912" maxBufferPoolSize="536870912" maxReceivedMessageSize="536870912" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://smtpsrvhk/SMTPService/SMTPService.asmx"
binding="basicHttpBinding" bindingConfiguration="SMTP ServiceSoap"
contract="SMTPService.SMTPServiceSoap" name="SMTP ServiceSoap" />
</client>
</system.serviceModel>
请帮助谢谢。
以下是我发送电子邮件的代码。
// Email Sending
public void sendEmail()
{
int result = -999;
//If Validated
if (CompareHash() == true)
{
MailMessage email = new MailMessage();
SmtpClient emailserv = new SmtpClient(SMTPserver);
emailserv.SendCompleted += new SendCompletedEventHandler(emailserv_SendCompleted);
//From / To / CC/ Bcc Handling
MailAddress sendfrom = returnEmailAddress(senderAddr, senderName);
email.Sender = sendfrom;
email.From = sendfrom;
foreach (var to in receipent)
{
email.To.Add(to);
}
foreach (var cc in copyToReceipent)
{
email.CC.Add(cc);
}
foreach (var bcc in blankcopyToReceipent)
{
email.Bcc.Add(bcc);
}
foreach (var reply in replytoReceipent)
{
email.ReplyToList.Add(reply);
}
if (ccToSender == true)
{
email.CC.Add(sendfrom);
}
if (bccToSender == true)
{
email.Bcc.Add(sendfrom);
}
//email.To.Add(sendto);
email.Subject = EmailTitle;
email.IsBodyHtml = useHTMLFormat;
email.Body = EmailContent;
//Add Attachment If found
if (attachmentPath.Count > 0)
{
for (int i = 0; i < attachmentPath.Count; i++)
{
string fileName = attachmentPath[i];
Attachment attachment = new Attachment(fileName);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(fileName);
disposition.ModificationDate = File.GetLastWriteTime(fileName);
disposition.ReadDate = File.GetLastAccessTime(fileName);
email.Attachments.Add(attachment);
}
}
emailserv.Credentials = null;
try
{
//emailserv.SendAsync(email, null);
emailserv.Send(email);
email.Dispose();
result = 1;
LogDown(result, "Email request has been sent to the SMTP server.");
}
catch (Exception ex)
{
result = -1;
LogDown(result, String.Format("Exception during Send, Message:{0}", ex.Message));
}
}
else
{
result = -2;
LogDown(result, "Hash Check Failed");
}
sendResult = result;
}
答案 0 :(得分:0)
据我所知,客户端和Web服务之间可能存在一些网络问题。
这是因为我在不同的位置设置了相同的网络服务,但效果很好。