我試圖從我的本地開發機器發送帶有圖像的測試電子郵件。該圖像未作爲附件發送。 可能是一個愚蠢的問題,但有沒有什麼辦法可以從我的本地機器上運行的網站發送電子郵件用於測試目的,然後再將其託管在www上。將圖像附加到電子郵件
public static void SendMail(string emailBody, string to, string from, string subject)
{
MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]");
mailMessage.Subject = subject;
mailMessage.Body = emailBody;
mailMessage.IsBodyHtml = true;
//mailMessage.Body += "<br /><br /> <asp:Image ID='Image1' runat='server' ImageUrl='~/images/banner.jpg' />";
string path = System.Web.HttpContext.Current.Server.MapPath(@"~/images/banner.jpg");
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><br/><img src=cid:companylogo/><br></body></html>" + emailBody, null, MediaTypeNames.Text.Html);LinkedResource logo = new LinkedResource(path);
av1.LinkedResources.Add(logo);
mailMessage.AlternateViews.Add(av1);
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand sc = new SqlCommand("SELECT EmailAdd FROM Volunteers where Country like 'United K%' and Race like 'Pak%' ", con);
con.Open();
SqlDataReader reader = sc.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
mailMessage.To.Add(reader[0].ToString());
}
}
reader.Close();
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
}
''有沒有什麼辦法可以在我的本地機器上運行我的網站發送電子郵件用於測試目的?' - 是的,絕對。運行本地SMTP偵聽器進行測試:http://smtp4dev.codeplex.com – David
你如何區分並且不會與像'cs'和'sc'這樣的變量混淆?我會更有意義地命名它們。 – abatishchev