Reputation: 814
Email------>Say [email protected]
Email1----->Say [email protected]
Email2----->Say [email protected]
Email3----->Say [email protected]
I am sending mail to this 4 ids along with image attachment.
The text with image is reaching to all 4 emailids.
The image (size 700kb) is displaying only for the emailid [email protected] but for rest emailids text is showing. Image is also reached but it is not opening (displaying) (image size (171B) (actual size 700kb)).
For the First emailid only, the image is displaying when trying to open it from the mail.
There are 4 foreach loop for all 4 emailids.
I am getting the following when trying to open the image:
Code:
The below code display the image in mail when trying to open the attatched image for [email protected]
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);
}
}
The below code does not display the image in mail for [email protected]
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);
}
}
}
I don't know what is wrong with the second foreach
loop, same for two other foreach
loops which I have not provided here.
Upvotes: 0
Views: 49
Reputation: 38850
I believe the issue is that you're reading from the same stream over and over, except after the first attempt you're reading from the end of the stream so you get no data.
A quick solution is to seek to the beginning of the stream before sending each e-mail:
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);
If you ever make your code parallel (i.e. multiple threads), note that you won't be able to use a single stream for this. Also note that not all streams are seekable (you can check their CanSeek
property).
If you're dealing with a non-seekable stream, you can (depending on the file size, memory constraints, etc.) either copy it to a MemoryStream
or save it to a file and read the file for each e-mail. See CopyTo method.
Upvotes: 2