使用如下代码可以发送附件
MailMessage mail = new MailMessage(); mail.To = ”me@mycompany.com”; mail.From = ”you@yourcompany.com”; mail.Subject = ”this is a test email.“; mail.Body = ”this is my test email body.“; MailAttachment attachment = new MailAttachment( Server.MapPath( ”mailTest.txt” ) ); //create the attachment mail.Attachments.Add( attachment ); SmtpMail.SmtpServer = ”localhost”; SmtpMail.Send( mail );
使用如下代码可以在用户上传附件后发生邮件:
if (FileUpload1.HasFile)
{
string toAddress = ”you@yourprovider.com”;
string fromAddress = you@yourprovider.com (2);
string mailServer = ”smtp.yourprovider.com”;
MailMessage myMailMessage = new MailMessage();
myMailMessage.To.Add(toAddress);
myMailMessage.From = new MailAddress(fromAddress);
myMailMessage.Subject = ”Test Message”;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Attachment myAttachment =
new Attachment(FileUpload1.FileContent, fileName);
myMailMessage.Attachments.Add(myAttachment);
SmtpClient mySmtpClient = new SmtpClient(mailServer);
mySmtpClient.Send(myMailMessage);
}