I'm trying to send mail with attachments using smtp client.
Everything goes well when I'm trying to add an attachment like that:
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(#"C:\icon.jpg");
mail.Attachments.Add(attachment);
but when I try to read a path from the console like:
string path = Console.Read();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(path);
mail.Attachments.Add(attachment);
I'm getting the exception
Illegal charcters in the path
Is there anyone who could explain to me why it doesn't work?
The problem with your code is that Console.Read() function is intended to read only the next character from the input.
You should use Console.ReadLine() instead, which will read an entire line from the input.
string path = Console.ReadLine();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(path);
mail.Attachments.Add(attachment);
Related
I am trying to attach a file saved on an FTP Server to a SMTP mail message.
mail.Body = body;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(Server.MapPath("Documents/quote.pdf"));
mail.Attachments.Add(attachment);
SmtpServer.Send(mail);
However, finding the attachment seems to trouble, due to an authentication issue. I am not quite sure if I should use a RequestStream and get the response, or if there is a way to authenticate the path for reading and adding the attachment to the email. The issue with the RequestStream is that I cant get the filename, which is what I need to add as a parameter when creating an attachment.
Any advise? Thanks in advance.
Use FtpWebRequest to obtain a Stream referring to a file contents on an FTP server. And then use an overload of Attachment constructor that takes a Stream.
const string filename = "quote.pdf";
var url = "ftp://ftp.example.com/remote/path/" + filename;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream contentStream = request.GetResponse().GetResponseStream();
Attachment attachment = new System.Net.Mail.Attachment(contentStream, filename);
When using the System.Net.Mail namespace to send a e-mail with attachment to any Yahoo account the attachment is downloaded with 'untitled' name instead the file name.
In the Yahoo Mail interface the attachment looks with the correct name but when you download it the download name goes to 'untitled' for all attachments. The same e-mail message works fine with Gmail, Outlook.com, Windows Live Mail and other clients.
Looking the raw message it constains a content-type with name but without filename attribute. The Yahoo works fine if the filename attribute is set but C# library don't use this.
That's the header generated by C# for attachments:
Content-Type: application/octet-stream; name=test.pdf
That's the header that works with Yahoo:
Content-Type: application/octet-stream; name=file2; filename=test.pdf
Anyone get this problem so far? Is there a work arround for C# default mail sending?
using (var message = new MailMessage("from#domain", "to#yahoo.com.br", "Test with attachment", "Test with attachment"))
{
var attachment = new Attachment(#"c:\temp\test.pdf"); // Same result using stream instead path to file.
attachment.Name = "test.pdf"; // Same result without this line.
message.Attachments.Add(attachment);
using (var smtp = new SmtpClient("smtp.domain", 587))
{
smtp.Credentials = new NetworkCredential("from#domain", "password");
smtp.Send(message);
}
}
I found a solution:
attachment.ContentDisposition.FileName = "test.pdf";
This add the missing filename attribute in the raw e-mail message and solve the Yahoo limitation.
Have you tried explicitly specifying the content type?
var attachment = new Attachment(... , MediaTypeNames.Application.Octet);
I am using a standard .NET SMTPClient to send a PLAIN text email - as follows:
// Configure mail client
using (SmtpClient mailClient = new SmtpClient(AppConfig.SMTPServer))
{
mailClient.Credentials = new System.Net.NetworkCredential(AppConfig.SMTPUsername, AppConfig.SMTPPassword);
// Create the mail message
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(AppConfig.SMTPSenderEmail, AppConfig.SMTPSenderDisplay);
foreach (string recipient in recipients)
{
mailMessage.To.Add(new MailAddress(recipient));
}
mailMessage.Bcc.Add(new MailAddress(AppConfig.SMTPBC));
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = false;
// Attachments
if (attachments != null && attachments.Any())
{
foreach (KeyValuePair<string, Byte[]> attachment in attachments)
{
MemoryStream memStream = new MemoryStream(attachment.Value);
mailMessage.Attachments.Add(new Attachment(memStream, attachment.Key));
}
}
mailClient.Send(mailMessage);
}
When sending through a POP3 client the email that is sent has the expected format, however when I send through the Azure SendGrid module, each new line is doubled up so there are two blank lines for every one in the source body string. Does anyone know how to circumvent this issue as I need (and want) to use SendGrid.
I see this very similar question SendGrid newline issue however the fix relates to PHP - I need the equivalent in C#
Ok, after several hours of investigation, within 5 minutes of posting the question I find the answer and its real simple, adding this to the mail client configuration sorts the issue perfectly:
mailMessage.BodyEncoding = Encoding.UTF8;
Currently we stored email text in db table and when we send an email, we query the db get the email text, do an HTML Encode and send email. But now we need to send Images in the email. What I did was stored the image in project file and stored the image location as tag in email text in db table. But its not working, any ideas on how should i do this. I need to insert image in middle of text. This is how we store html email in db table. As html is being parsed I copied it in comments section.
Pass your body to an html form and allowhtml(true) while sending email like this
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Host = Host;
mail.IsBodyHtml = true;
mail.From = new MailAddress(FromEmail);
mail.To.Add(ToEmail);
mail.Body = MailBody;
such that MailBody is String.Format("{0} with < img href='{1}' />",email,imagesrc);
{0} will be replaced by email
{1} will be replaced by imagesrc
you have to upload your picture to a webserver and set the src of the image to the absolute path!
<img src="http://www.myuploaded.com/image.jpg" />
if you cannot do that look at this post:
how to embed image in email
I'm using system.net.mail and have a textbox that users can enter their email address and a file gets attached and sent to them. When I test this in my custom box with Server 2008 I get the following error:
Request for the permission of type 'System.Net.Mail.SmtpPermission....at System.Security.CodeAccessSecurityEngine.Check
Do I have to configure something specifically on the server to allow? Or is it a code error?
string strto = txtTo.Text;
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("serveremail");
mail.To.Add(strto);
//set the content
mail.Subject = "subject";
//Get some binary data
byte[] byteArray = Encoding.ASCII.GetBytes(result);
//save the data to a memory stream
using (MemoryStream ms = new MemoryStream(byteArray))
//create the attachment from a stream. Be sure to name the data with a file and
//media type that is respective of the data
mail.Attachments.Add(new Attachment(ms, "test.txt", "text/plain"));
//send the message
SmtpClient smtp = new SmtpClient("server");
smtp.Send(mail);
This looks like it could be a permission issue when trying to access the attachment from the steam. What happens if you explicitly state credentials?