I made a console application to test MimeKit, it's executed without errors but emails never arrives.
i usae same data with System.Net.Mail and all works fine.
What can be the problem?
MimeKit code:
var email = new MimeMessage();
email.Sender = MailboxAddress.Parse("aa#sbb.com");
email.To.Add(MailboxAddress.Parse("aa.cc#dd.com"));
email.To.Add(MailboxAddress.Parse("bb.dd#dd.com"));
email.Subject = "test";
var builder = new BodyBuilder();
builder.HtmlBody = "corpo";
email.Body = builder.ToMessageBody();
using var smtp = new SmtpClient();
smtp.Connect("smtp.xxx.com", 25, SecureSocketOptions.None);
smtp.Authenticate("ut", "pwd");
smtp.SendAsync(email);
smtp.Disconnect(true);
Console.WriteLine("Email Sent.");
System.mail code:
using (MailMessage mm = new MailMessage())
{
mm.To.Add(new MailAddress("aa.cc#dd.com"));
mm.To.Add(new MailAddress("bb.dd#dd.com"));
mm.From = new MailAddress("aa#sbb.com", "You");
mm.ReplyToList.Add("aa#sbb.com");
mm.Subject = "vecchia mail";
mm.Body = "con system.mail";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.xxx.com";
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential NetworkCred = new NetworkCredential("ut", "pwd");
smtp.Credentials = NetworkCred;
smtp.Port = 25;
Console.WriteLine("Sending Email......");
smtp.Send(mm);
Console.WriteLine("Email Sent.");
}
Thanks to #Klaus Gütter i got the error and then find the solution: a security rule that prevent connection between development machine and mail server, once deployed the application in test environment all works fine.
thanks for suggestions
Here is My Export code.
public void ExportCSV_Employee(HttpPostedFileBase file )
{
var sb = new System.Text.StringBuilder();
var list = _context.ProductItems.ToList();
foreach (var item in list)
{
sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}\t{17}\t{18}\t{19}\t{20}\t{21}\t{22}\t{23}\t{24}\t{25}\r", item.ScanCode, item.Name, item.UnitRetail, item.UnitCost, item.DeptName, item.PriceGroup, item.ProductCode, item.Pcode, item.StateTax, item.MaxQty, item.Modified, item.AllowFoodStamps, item.LocalTax, item.Crv, item.MinAge, item.AllowDirectDept, item.IsNegative, item.DeptType, item.ItemCategoryId, item.ProductSubCategory, item.AllowFractionDept, item.BuyDown, item.UpcType, item.OverRide, item.PkgSize, item.Discount);
}
//mail method to send mail
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("mymail#gmail.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mymail#gmail.com" , "password");
smtp.Host = "smtp.gmail.com";
//recipient address
mail.To.Add(new MailAddress("receiver#gmail.com"));
}
in this way, my mail sending work but I want to attach the date from my database as a start retrieving through StringBuilder. How can I attach my data as a file attachment with email?
var sb = new System.Text.StringBuilder();
var list = _context.ProductItems.ToList();
foreach (var item in list)
{
sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}\t{17}\t{18}\t{19}\t{20}\t{21}\t{22}\t{23}\t{24}\t{25}\r", item.ScanCode, item.Name, item.UnitRetail, item.UnitCost, item.DeptName, item.PriceGroup, item.ProductCode, item.Pcode, item.StateTax, item.MaxQty, item.Modified, item.AllowFoodStamps, item.LocalTax, item.Crv, item.MinAge, item.AllowDirectDept, item.IsNegative, item.DeptType, item.ItemCategoryId, item.ProductSubCategory, item.AllowFractionDept, item.BuyDown, item.UpcType, item.OverRide, item.PkgSize, item.Discount);
}
//Create a virtual file in memory
byte[] bytes = null;
using (var ms = new MemoryStream())
{
TextWriter tw = new StreamWriter(ms);
tw.Write(sb.Tostring());
tw.Flush();
ms.Position = 0;
bytes = ms.ToArray();
}
//Create Attachment
Attachment att = new Attachment(new MemoryStream(bytes), name);
//mail method to send mail
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("mymail#gmail.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mymail#gmail.com" , "password");
smtp.Host = "smtp.gmail.com";
//recipient address
mail.To.Add(new MailAddress("receiver#gmail.com"));
//Add the attachment to the mail message
mail.Attachments.Add(data);
After spending some time I figure out the solution for this problem first save data in a Memorystream and then pass it to the email attachment method.
{
public void ExportCSV_Employee(HttpPostedFileBase file )
{
var sb = new System.Text.StringBuilder();
var list = _context.ProductItems.ToList();
foreach (var item in list)
{
sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}\t{17}\t{18}\t{19}\t{20}\t{21}\t{22}\t{23}\t{24}\t{25}\r", item.ScanCode, item.Name, item.UnitRetail, item.UnitCost, item.DeptName, item.PriceGroup, item.ProductCode, item.Pcode, item.StateTax, item.MaxQty, item.Modified, item.AllowFoodStamps, item.LocalTax, item.Crv, item.MinAge, item.AllowDirectDept, item.IsNegative, item.DeptType, item.ItemCategoryId, item.ProductSubCategory, item.AllowFractionDept, item.BuyDown, item.UpcType, item.OverRide, item.PkgSize, item.Discount);
}
//Storing data in virtual memory and then pass to email attachment
var myString = sb.ToString();
var myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
var ms = new MemoryStream(myByteArray);
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("mymail#gmail.com");
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mymail#gmail.com" , "password");
smtp.Host = "smtp.gmail.com";
//recipient address
mail.To.Add(new MailAddress("receiver#gmail.com"));
string Fname = "PriceBook.dat";
if ( ms!= null)
{
string fileName = Fname;
mail.Attachments.Add(new Attachment(ms, "fileName.dat", "text/dat"));
};
smtp.Send(mail);
}
}
I have to send mail with multiple cc addresses.
For now this code only works with one.
Here's the code:
MailAddress addressTo = new MailAddress("ma#.asd.a");
MailAddress addressFrom = new MailAddress("ma#.asd.a");
MailMessage MyMessage = new MailMessage("ma#.asd.a", "ma#.asd.a");
MyMessage.Subject = "New requests";
MyMessage.Body = #"Body";
MailAddress copy = new MailAddress("ma#.asd.a");
MyMessage.CC.Add(copy);
SmtpClient client = new SmtpClient();
client.Port = 32;
client.EnableSsl = false;
client.Host = "my-smtp";
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential(LocalUserName, "");
client.UseDefaultCredentials = false;
client.Send(MyMessage);
I allready tyed this:
MailAddress copy2 = new MailAddress("maas#.asd.a");
MyMessage.CC.Add(copy2)
But it's not working the request has time out error.
Can someone help me with this ?
Just add them individually:
MyMessage.CC.Add("a#a.com");
Or add multiple as a AddRange:
MyMessage.CC.AddRange("a#a.com", "b#b.com");
This is a class from my code.
string to = message.Text;
string from = "noreply#zayzaycorporation.onmicrosoft.com";
MailMessage messagex = new MailMessage(from, to);
string mailbody = "Welcome!";
messagex.Subject = "Hello";
messagex.Body = mailbody;
messagex.Attachments.Add(new System.Net.Mail.Attachment("C:\\doc\\start.pdf"));
messagex.BodyEncoding = Encoding.UTF8;
messagex.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("noreply#zayzaycorporation.onmicrosoft.com", "XXX");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
client.Send(messagex);
context.Done(string.Empty);
The class sends the email with an attachment, works good. But my problem is when it's published as a Web Service in azure because there doesn't find any folder named "doc" on "C:\\". How can it be referenced the attachment to a folder from the main solution?
Thanks.
The Attachment class can be constructed using a Stream, which can thus contain anything and come from anywhere.
So basically generate the attachment in memory and wrap it in a MemoryStream or similar.
Try this..
String path = Application.StartupPath + "\doc\test.xml";
I've been looking all over the net to attach a print document in an email using SMTP but unlucky couldn't find one.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
Here's my code for previewing the document , I have a extra method for placing all labels,images in paper.
private void btnSendEmail_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("myUsername#gmail.com", "myPass");
MailMessage mm = new MailMessage("myRecipents", "myRecipents", "Title", "Body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = "tange.pdf";
mm.Attachments.Add(new Attachment("C:/tange.pdf", contentType));
client.Send(mm);
MessageBox.Show("Success");
}
Here's my code to send email and attach document. But here the document attached here is located in my disk. I want to replace that with my print document/preview.
Is there any way to make that?