我使用this website来设置我的电子邮件服务,但似乎工作得很好但我无法弄清楚如何使用电子邮件和cc两个电子邮件地址添加pdf。
C#代码部分:
EmailServiceClient emailClient = new EmailServiceClient();
emailClient.SendEmailCompleted +=
new EventHandler<SendEmailCompletedEventArgs>(emailClient_SendEmailCompleted);
//MessageBox.Show(SelectedFeatureLayer.ID);
if (SelectedFeatureLayer.ID == "AllAvailableFeatureLayer" ||
SelectedFeatureLayer.ID == "AMAvailableLayer") {
if (msgToAM.Length != 0 & msgToAM != "Type email here" &
Regex.IsMatch(msgToAM, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")) {
//MessageBox.Show("create new message");
SLEmailMessage AMemailMessage = new SLEmailMessage {
To = msgToAM,
From = "name@email.org",
Subject = "Thanks to Bike Count Volunteers",
Body = AMmsgBody
};
emailClient.SendEmailAsync(AMemailMessage);
}
}
答案 0 :(得分:0)
这可能有所帮助:
http://www.codeproject.com/Questions/425435/WCF-email-web-service-error-exception-when-sending
答案 1 :(得分:0)
事实证明我需要添加以下内容,并重新发布我的应用并更新我的服务参考:
SLEmailMessage.cs:
[DataContract]
public class SLEmailMessage
{
[DataMember]
public string To { get; set; }
[DataMember]
public string From { get; set; }
[DataMember]
public string Subject { get; set; }
[DataMember]
public string Body { get; set; }
[DataMember]
public string CC { get; set; }
[DataMember]
public string Bcc { get; set; }
[DataMember]
public string Attachment { get; set; }
}
EmailService.svc.cs:
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmailService
{
[OperationContract]
public bool SendEmail(SLEmailMessage emailMessage)
{
bool IsEmailSendSuccessful = false;
try
{
MailMessage mailMessage = new MailMessage(emailMessage.From, emailMessage.To);
mailMessage.CC.Add(emailMessage.CC);
mailMessage.Bcc.Add(emailMessage.Bcc);
var pdfMessage = new Attachment(emailMessage.Attachment);
pdfMessage.ContentDisposition.FileName="BikeCountInfo.pdf";
mailMessage.Attachments.Add(pdfMessage);
mailMessage.Subject = emailMessage.Subject;
mailMessage.Body = emailMessage.Body;
SmtpClient smtp = new SmtpClient();
smtp.Send(mailMessage);
IsEmailSendSuccessful = true;
}
catch
{
IsEmailSendSuccessful = false;
}
return IsEmailSendSuccessful;
}
// Add more operations here and mark them with [OperationContract]
}
更新服务并重新发布应用 - 然后添加:
MainPage.xaml.cs中:
if (SelectedFeatureLayer.ID == "AllAvailableFeatureLayer" || SelectedFeatureLayer.ID == "PMAvailableLayer")
{
if (msgToPM.Length != 0 & Regex.IsMatch(msgToPM, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
{
SLEmailMessage PMemailMessage = new SLEmailMessage
{
To = msgToPM,
From = "email@name.org",
CC = "email@name.org",
Bcc = "email@name.org",
Attachment = "location of attachment",
Subject = "Thanks to Bike Count Volunteers",
Body = PMmsgBody
};
emailClient.SendEmailAsync(PMemailMessage);
}