为什么我不能发送 xls、doc 和其他文件 - 它确实适用于 jpg、txt 和其他文件。
private void BuildAndSend(string pTo,string pCC,string pSubject,string pBody)
{
// building the mail
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(pTo);
System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("mymail@gmail.com");
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);
mm.Subject = pSubject ;
mm.Body = pBody;
System.Net.Mail.MailAddress cc = new System.Net.Mail.MailAddress(pCC);
mm.CC.Add(cc);
addAttachments(mm);
mm.IsBodyHtml = true;
mm.BodyEncoding = System.Text.Encoding.UTF8;
//sending the mail
sendMail(mm);
}
private void addAttachments(System.Net.Mail.MailMessage mm)
{
string attachmentFile;
for (int i = 0; i < lstAttachments.Items.Count ; i++)
{
string fileFullName = pullDictionary[i];
attachmentFile = fileFullName;
System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(attachmentFile);
mm.Attachments.Add(mailAttachment);
}
}
private void sendMail(System.Net.Mail.MailMessage mm)
{
try
{
// loging in into sending user account
string smtpHost = "smtp.gmail.com";
string userName = "mymail@gmail.com";//sending Id
string password = "mypass";
System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
mClient.Port = 587;
mClient.EnableSsl = true;
mClient.UseDefaultCredentials = false;
mClient.Credentials = new NetworkCredential(userName, password);
mClient.Host = smtpHost;
mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mClient.Send(mm);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
如果您能告诉我另一种发送这些文件的方法,那也很棒