Benutzer:MovGP0/Email
| MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | MSCert | Physik |
|
create and send Email[1]using System.Net.Mail;
using System.IO;
public static CreateAndSendMessage(...)
{
var file = "data.xls";
// create the message
using(var message = new MailMessage(from, to, subject, body))
{
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
// create attachment
using(var attachment = new Attachment(file, MediaTypeNames.Application.Octet))
{
var disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(file);
disposition.ModificationDate = File.GetLastWriteTime(file);
disposition.ReadDate = File.GetLastAccessTime(file);
message.Attachments.Add(attachment);
// send the message.
var smtpServerName = ConfigurationManager.AppSettings["smtpServer"];
var port = int.Parse(ConfigurationManager.AppSettings["smtpPort"]);
var useSsl = bool.Parse(ConfigurationManager.AppSettings["smtpSSL"]);
using(var client = new SmtpClient(server, port))
{
client.EnableSsl = useSsl;
// customCredentials
client.UseDefaultCredentials = false;
client.Credentials = CredentialCache.DefaultNetworkCredentials; // new NetworkCredential(@"DOMAIN\USER", "PASSWORD");
//// DON'T: ignore invalid certificate
// ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;
client.Send(message);
}
}
}
}
Inline Imageusing (var client = new SmtpClient())
{
MailMessage newMail = new MailMessage();
newMail.To.Add(new MailAddress("[email protected]"));
newMail.Subject = "Test Subject";
newMail.IsBodyHtml = true;
var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
inlineLogo.ContentId = Guid.NewGuid().ToString();
string body = string.Format(@"<img src=""cid:{0}"" />", inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
newMail.AlternateViews.Add(view);
client.Send(newMail);
}
app.config / web.configDer Server kann deklarativ konfiguriert werden:[2] <configuration>
<system.net>
<mailSettings>
<!-- configure SMTP server -->
<smtp deliveryMethod="network">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
<!-- use SSL; use custom credentials -->
<!--
<smtp deliveryMethod="network">
<network host="localhost" port="465" defaultCredentials="false" enableSsl="true" />
</smtp>
-->
<!-- deliver mails to File System -->
<!--
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\foo\emails\" />
</smtp>
-->
</mailSettings>
</system.net>
</configuration>
Quellen
Tools
|
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.