請給我一個代碼發送郵件腳本在asp.net與C#。我想建立查詢表格在哪裏我想發送我的電子郵件ID與附件,主題和正文的所有信息。在asp.net發送郵件腳本用c#
2
A
回答
6
try
{
// Assign a sender, recipient and subject to new mail message
MailAddress sender =
new MailAddress("[email protected]", "Sender");
MailAddress recipient =
new MailAddress("[email protected]", "Recipient");
MailMessage m = new MailMessage(sender, recipient);
m.Subject = "Test Message";
// Define the plain text alternate view and add to message
string plainTextBody =
"You must use an email client that supports HTML messages";
AlternateView plainTextView =
AlternateView.CreateAlternateViewFromString(
plainTextBody, null, MediaTypeNames.Text.Plain);
m.AlternateViews.Add(plainTextView);
// Define the html alternate view with embedded image and
// add to message. To reference images attached as linked
// resources from your HTML message body, use "cid:contentID"
// in the <img> tag...
string htmlBody =
"<html><body><h1>Picture</h1><br>" +
"<img src=\"cid:SampleImage\"></body></html>";
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(
htmlBody, null, MediaTypeNames.Text.Html);
// ...and then define the actual LinkedResource matching the
// ContentID property as found in the image tag. In this case,
// the HTML message includes the tag
// <img src=\"cid:SampleImage\"> and the following
// LinkedResource.ContentId is set to "SampleImage"
LinkedResource sampleImage =
new LinkedResource("sample.jpg",
MediaTypeNames.Image.Jpeg);
sampleImage.ContentId = "SampleImage";
htmlView.LinkedResources.Add(sampleImage);
m.AlternateViews.Add(htmlView);
// Finally, configure smtp or alternatively use the
// system.net mailSettings
SmtpClient smtp = new SmtpClient
{
Host = "smtp.bigcompany.com",
UseDefaultCredentials = false,
Credentials =
new NetworkCredential("username", "password")
};
//<system.net>
// <mailSettings>
// <smtp deliveryMethod="Network">
// <network host="smtp.bigcompany.com"
// port="25" defaultCredentials="true"/>
// </smtp>
// </mailSettings>
//</system.net>
smtp.Send(m);
}
catch (ArgumentException)
{
throw new
ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
throw new
FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
throw new
InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
throw new SmtpFailedRecipientException(
"The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
// Invalid hostnames result in a WebException InnerException that
// provides a more descriptive error, so get the base exception
Exception inner = ex.GetBaseException();
throw new SmtpException("Could not send message: " + inner.Message);
}
1
1
相關問題
- 1. 如何發送郵件在Asp.net用C#
- 2. ASP.NET簡單郵件發送使用C#
- 3. 發送電子郵件使用asp.net,C#
- 4. 發送電子郵件在ASP.NET C#
- 5. 在asp.net中發送郵件失敗C#
- 6. 在asp.net中發送郵件通知c#
- 7. VB腳本發送郵件 - 等到發送郵件
- 8. 使用shell腳本發送HTML郵件
- 9. 用PHP腳本發送HTML郵件
- 10. 用Java腳本發送郵件?
- 11. 使用VB腳本發送郵件?
- 12. asp.net發送郵件
- 13. ASP.NET:發送郵件
- 14. 用ASP.NET發送郵件
- 15. 使用ASP.NET發送郵件
- 16. 在asp.net發送郵件
- 17. 在asp.net中發送郵件
- 18. asp.net發送郵件在vb.net
- 19. 在asp.net中發送郵件?
- 20. c#asp.net發送郵件問題
- 21. 發送電子郵件asp.net c#
- 22. 從Asp.Net/C#發送郵件時出錯
- 23. sql asp.net發送電子郵件c#
- 24. 用C#ASP.NET發送郵件的電子郵件SMTP PORT
- 25. 本地郵件發送,但上傳到服務器郵件不發送在asp.net
- 26. 用C#發送郵件附件郵件
- 27. 發送電子郵件從PHP腳本
- 28. 發送郵件谷歌腳本
- 29. 從Ubuntu的shell腳本發送郵件
- 30. 發送電子郵件的腳本