如何发送带有图像附件的多个ID的邮件?

时间:2019-01-15 09:08:59

标签: c#

Email------>Say Xyz@gmail.com
Email1----->Say Abc@gmail.com
Email2----->Say cid@gmail.com
Email3----->Say wer@gmail.com

我正在向这4个ID以及图片附件发送邮件。

带有图像的文本将到达所有4个电子邮件ID。

仅针对emailid Xyz@gmail.com显示图像(大小为700kb),而对于其余的emailid显示文本。图像也到达了,但没有打开(显示)(图像大小(171B)(实际大小700kb))。

仅对于第一个emailid,尝试从邮件中打开图像时会显示该图像。

所有4个emailid都有4个foreach循环。

尝试打开图像时出现以下提示:

https://ibb.co/PZyFJMp

代码:

当尝试打开Xyz@gmail.com的附加图像时,以下代码显示邮件中的图像

foreach (string email in emails)
            {

                string SendEmail = email;
                idno = idno + 1;

                try
                {

                    MailMessage message = new MailMessage(from, SendEmail, txtsubject.Text, myString);
                    message.From = new MailAddress(from, fromname);
                    message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                    message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    message.Headers.Add("Disposition-Notification-To", from);
                    message.IsBodyHtml = true;
                    if (fuAttach.HasFile)
                    {
                        filename = Path.GetFileName(fuAttach.PostedFile.FileName);
                        fuAttach.SaveAs(Server.MapPath("~/expo_crm/Attachments/" + filename));
                        Attachment data = new Attachment(fuAttach.PostedFile.InputStream, filename);
                        message.Attachments.Add(data);
                    }
                    smtp.Send(message);

                    object userid = user_ids[idno];
                    // int tst = Convert.ToInt(userid);

                    // int uid = Convert.ToInt16(user_ids[idno]);
                    Int32 uid = Convert.ToInt32(user_ids[idno]);
                    sent_user_ids.Add(uid);
                }

                catch (Exception ex)
                {

                    errorno++;
                    object userid = user_ids[idno];
                    // int tst = Convert.ToInt(userid);
                   // int uid = Convert.ToInt16(user_ids[idno]);

                    Int32 uid = Convert.ToInt32(user_ids[idno]);
                    Notsent_user_ids.Add(uid);
                }
            }

以下代码不显示Abc@gmail.com邮件中的图像

idno = -1;
            foreach (string email in emails1)
            {
                string MyEmail1 = email;
                idno = idno + 1;
                if (MyEmail1 != "N/A")
                {
                    try
                    {

                        MailMessage message = new MailMessage(from, MyEmail1, txtsubject.Text, myString);
                        message.From = new MailAddress(from, fromname);
                        message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                        message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                        message.Headers.Add("Disposition-Notification-To", from);
                        message.IsBodyHtml = true;
                        if (fuAttach.HasFile)
                        {
                            filename = Path.GetFileName(fuAttach.PostedFile.FileName);
                            fuAttach.SaveAs(Server.MapPath("~/expo_crm/Attachments/" + filename));//added by chetan
                            Attachment data = new Attachment(fuAttach.PostedFile.InputStream, filename);
                            message.Attachments.Add(data);
                        }
                        smtp.Send(message);
                        sent1++;




                    }

                    catch (Exception ex)
                    {
                        object userid = user_ids[idno];
                        // int tst = Convert.ToInt(userid);
                        // int uid = Convert.ToInt16(user_ids[idno]);
                        Int32 uid = Convert.ToInt32(user_ids[idno]);
                        notsend1.Add(uid);
                    }
                }
            }

我不知道第二个foreach循环出了什么问题,与其他两个foreach循环(在此未提供)一样。

1 个答案:

答案 0 :(得分:2)

我认为问题在于,您一次又一次地从同一流中读取数据,除非您是第一次尝试从流的末尾读取数据,所以您没有任何数据。

一种快速的解决方案是在发送每封电子邮件之前搜索流的开头:

filename = Path.GetFileName(fuAttach.PostedFile.FileName);
fuAttach.SaveAs(Server.MapPath("~/expo_crm/Attachments/" + filename));//added by chetan
fuAttach.PostedFile.InputStream.Seek(0, SeekOrigin.Begin); // added seek
Attachment data = new Attachment(fuAttach.PostedFile.InputStream, filename);
message.Attachments.Add(data);

如果您使代码并行(即多个线程),请注意,您将无法为此使用单个流。另外请注意,并非所有流都是可搜索的(您可以检查其CanSeek属性)。

如果您正在处理不可搜索的流,则可以(取决于文件大小,内存限制等)将其复制到MemoryStream或将其保存到文件并读取该文件每封电子邮件。参见CopyTo method