如何在 C# 和 asp.net 中创建电子邮件附件。我想使用一个描述附件的 html 文件,并且我希望它在我的应用程序中加载一种消息字符串。比我想用从数据库中获得的其他值替换此消息中的子字符串。如果附件已创建,我想将其发送到一个地址。
我现在使用帮助类,但我认为这不是正确的方法:/
我不知道它是否存在于 .net 库中。一种类什么的。
最好的方法是什么?
这是我现在的方法:namespave = using SmtpMail = EASendMail.SmtpMail;
private void SendMail(string vorname, string nachname, string anrede, string firma, string benutzername, string passwort, string von, string bis, string email)
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
oMail.From = email;
oMail.To = email;
oMail.Subject = "Company (" + nachname + ", " + vorname + ")";
SmtpServer oServer = new SmtpServer(SMTPSERVER);
try
{
Attachment header = oMail.AddAttachment(Properties.Settings.Default.ATT_header);
Attachment footer = oMail.AddAttachment(Properties.Settings.Default.ATT_footer);
Attachment left = oMail.AddAttachment(Properties.Settings.Default.ATT_left);
Attachment right = oMail.AddAttachment(Properties.Settings.Default.ATT_right);
Attachment world = oMail.AddAttachment(Properties.Settings.Default.ATT_world);
Attachment company = oMail.AddAttachment(Properties.Settings.Default.ATT_company);
Attachment weltkarte_header = oMail.AddAttachment(Properties.Settings.Default.ATT_weltkarte);
string contentID_header = "header";
header.ContentID = contentID_header;
string contentID_footer = "footer";
footer.ContentID = contentID_footer;
string ContentID_left = "left";
left.ContentID = ContentID_left;
string ContentID_right = "right";
right.ContentID = ContentID_right;
string ContentID_world = "world";
world.ContentID = ContentID_world;
string ContentID_company = "company";
company.ContentID = ContentID_company;
string ContentID_weltkarte_header = "weltkarte_header";
weltkarte_header.ContentID = ContentID_weltkarte_header;
string htmltext = "<html><body><table width='1000px' border='0' cellpadding='0' cellspacing='0'>" +
"<tr><img src=\"cid:" + contentID_header + "\"></tr>" +
"<tr><img src=\"cid:" + ContentID_weltkarte_header + "\"></tr>" +
"<tr><table border='0' cellpadding='0' cellspacing='0'>" +
"<tr>" +
"<td><img src=\"cid:" + ContentID_left + "\"></td>" +
"<td width='880' style='background-color:#efefef;'>" +
"<p align='center'>Sie haben einen Gastzugang für [Anrede] [Vorname] [Nachname],[Firma] eingerichtet.</p>" +
"<p align='center'>Im folgenden finden Sie die Zugangsdaten,</br>" +
"die für die Anmeldung am Netzwerk benötigt werden.Weitere Informationen stehen auf der Anmeldeseite zur Verfügung.</p>" +
"<p align='center'><b>Benutzername: [Benutzername]</b><br/><b>Kennwort: [Passwort]</b></p>" +
"<p align='center'>Der Zugang wird vom [ZeitVon] bis [ZeitBis] freigeschaltet sein.</p>" +
"</td>" +
"<td><img src=\"cid:" + ContentID_right + "\"></td>" +
"</tr>" +
"</table></tr>" +
"<tr><img src=\"cid:" + ContentID_company + "\"></tr>" +
"<tr><img src=\"cid:" + contentID_footer + "\"></tr>" +
"</table></body></html>";
htmltext = htmltext.Replace("[Anrede]", anrede).Replace("[Vorname]", vorname).Replace("[Firma]", firma).Replace("[Nachname]", nachname);
htmltext = htmltext.Replace("[Benutzername]", benutzername).Replace("[Passwort]", passwort);
htmltext = htmltext.Replace("[ZeitVon]", von).Replace("[ZeitBis]", bis);
oMail.HtmlBody = htmltext;
oSmtp.SendMail(oServer, oMail);
}
catch (Exception)
{
}
}
更新:
现在我已经创建了一个带有图像的 html 文件,但是我用 base64 编码绑定了这些图像。它有效,但如果我在 c# 应用程序中阅读此 html,我将无法发送此邮件。我做了一个断点并查看我的读取字符串,但没关系:/
这里的代码:
...
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
oMail.From = mail;
oMail.To = mail;
oMail.Subject = "company (" + lastname + ", " + firstname + ")";
SmtpServer oServer = new SmtpServer(SMTPSERVER);
try
{
using (StreamReader reader = new StreamReader(Server.MapPath("~/App_Data/zugangsmail.html"), System.Text.Encoding.Default))
{
string message = reader.ReadToEnd();
message = message.Replace("[Anrede]", title).Replace("[Vorname]", firstname).Replace("[Firma]", company).Replace("[Nachname]", lastname);
message = message.Replace("[Benutzername]", username).Replace("[Passwort]", password);
message = message.Replace("[ZeitVon]", from).Replace("[ZeitBis]", to);
oMail.HtmlBody = message;
oSmtp.SendMail(oServer, oMail);
}
}
catch (Exception ex)
{
error.Visible = true;
lblErrorMessage.Text = ex.Message;
}
...