Comment puis-je joindre une image dans la teneur en corps. J'ai écrit le code ci-dessous Le code est bien car je reçois le message aussi, mais l'image arrive comme [x] à l'intérieur du corps et non comme l'image.
Comment résoudre ceci? Le chemin est correct? P> p>
3 Réponses :
Utilisez appelé Si le tutoriel n'aide pas, ne soyez pas timide et demandez des éclaircissements. :) p> linkedresource code>. ici Vous pouvez trouver comment-à-faire. Ont fait cela avec succès. P>
string attachmentPath = Environment.CurrentDirectory + @"\test.png"; Attachment inline = new Attachment(attachmentPath); inline.ContentDisposition.Inline = true; inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; inline.ContentId = contentID; inline.ContentType.MediaType = "image/png"; inline.ContentType.Name = Path.GetFileName(attachmentPath); message.Attachments.Add(inline); reference: Send an Email in C# with Inline attachments
Oh ... Cette approche semble encore meilleure.
Pour quiconque en regardant cette solution à l'avenir, afin de le faire fonctionner, je devais ajouter la balise d'image suivante dans mon code (mon contenu est "Capture d'écran"): message.body = " "; code>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Net.Mime; using System.Net.Mail; namespace ItsTrulyFree { public partial class demo_mail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { enter code here } protected void btnSubmit_Click(object sender, EventArgs e) { MailMessage Msg = new MailMessage(); // Sender e-mail address. Msg.From = new MailAddress(txtUsername.Text); // Recipient e-mail address. Msg.To.Add(txtTo.Text); Msg.Subject = txtSubject.Text; // File Upload path String FileName = fileUpload1.PostedFile.FileName; string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>"; //LinkedResource LinkedImage = new LinkedResource(FileName); //HttpContext.Current.Server.MapPath("/UploadedFiles"); LinkedResource LinkedImage = new LinkedResource(Server.MapPath("~//" + FileName), "image/jpg"); LinkedImage.ContentId = "MyPic"; //Added the patch for Thunderbird as suggested by Jorge LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailbody+ " <img src=cid:MyPic>", null, "text/html"); htmlView.LinkedResources.Add(LinkedImage); Msg.AlternateViews.Add(htmlView); SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text); smtp.EnableSsl = true; smtp.Send(Msg); Msg = null; Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>"); } //catch (Exception ex) //{ // Console.WriteLine("{0} Exception caught.", ex); //} } }
Merci j'ai résolu le message.body = "";
Non tu n'es pas. Les chances que le destinataire contiennent ce fichier stocké dans le répertoire racine sont zéro.
@Hanspassant: à moins que l'e-mail ne soit jamais envoyé à l'OP! ;-)