Thanks for looking at my question.
I am trying to figure out attachments for OpenNetCF.Net.Mail. Here is the code for my SendMail function:
public static void SendMessage(string subject,
string messageBody,
string fromAddress,
string toAddress,
string ccAddress)
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
MailAddress address = new MailAddress(fromAddress);
// Set the sender's address
message.From = address;
// Allow multiple "To" addresses to be separated by a semi-colon
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))
{
message.To.Add(new MailAddress(addr));
}
}
// Allow multiple "Cc" addresses to be separated by a semi-colon
if (ccAddress.Trim().Length > 0)
{
foreach (string addr in ccAddress.Split(';'))
{
message.CC.Add(new MailAddress(addr));
}
}
// Set the subject and message body text
message.Subject = subject;
message.Body = messageBody;
// TODO: *** Modify for your SMTP server ***
// Set the SMTP server to be used to send the message
client.Host = "smtp.dscarwash.com";
string domain = "dscarwash.com";
client.Credentials = new SmtpCredential("mailuser", "dscarwash10", domain);
// Send the e-mail message
try
{
client.Send(message);
}
catch (Exception e)
{
string data = e.ToString();
}
}
It is supposed to be a matter of tweaking it in the following way to allow attachments:
Attachment myAttachment = new Attachment();
message.Attachments.Add(myAttachment);
The problem is that I cannot figure out how to get the attachment to add. The lines above should be it with something else in the middle where I actually tell it what file I would like to attach. Any help on this matter will be greatly appreciated.
Thanks again!
As per this MSDN documentation, you can specify the filename of the attachment as a parameter. Thus, you can give the fullpath as the string parameter.
They have AttachmentBase which can be used to construct a email attachment. However, we cannot add the instance of AttachmentBase to the attachments of email message.
I think the Attachment class should inherit from AttachmentBase. I think this maybe a defect.
Related
I am trying to set the 'return-path' for my emails but I'm not seeing it as an available parameter. It seems like replytolist is not the same thing. I wan't to set the location that bounced emails are delivered. Here is my code so far:
private static void SendMail(string html,string taxId,string toEmail,string filePath,string fromEmail,string replyToEmail,string emailSubject,string emailAttachPath)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = emailSubject;
mail.Body = html;
//specify the priority of the mail message
mail.ReplyToList.Add(replyToEmail);
SmtpClient SmtpServer = new SmtpClient("smtp.server.com");
SmtpServer.Port = 25;
SmtpServer.UseDefaultCredentials = true;
SmtpServer.EnableSsl = false;
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
If you dont write return path emailaddress. Server will take from email address convert into the return path. You also can see email report in your email original source.
If you want to add custom reutn path you can use
MailMessage message = new MailMessage();
message.Headers.Add("Return-Path", "response#*****.biz");
Also if you using postfix and you want add return path Automatically You will have to make changes in two files
canonical :- put your return path emailaddress here.
//reply#domain.com
main.cf :- write your code in main.cf file
canonical_classes = envelope_sender
sender_canonical_maps = regexp:/etc/postfix/canonical
I'm trying to add an image in HTML mail. It is displaying when I save that body as html file, but when I send that html body as an email through GMail, the image is not displaying. Can anyone tell me the reason?
I set the source of the image in this way:
var image = body.GetElementsByTagName("img");
string imageAttachmentPath = Path.Combine(Globals.NotificationTemplatesPath, "Header.png");
foreach (XmlElement img in image)
{
img.SetAttribute("src", imageAttachmentPath);
break;
}
//thats the method in which i am sending email.
public static void SendMessageViaEmailService(string from,
string sendTo,
string carbonCopy,
string blindCarbonCopy,
string subject,
string body,
bool isBodyHtml,
string imageAttachmentPath,
Hashtable images,
List<string> attachment,
string title = null,
string embeddedImages = null)
{
Attachment image = new Attachment(imageAttachmentPath);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true; // email body will allow html elements
msg.From = new MailAddress(from, "Admin"); // setting the Sender Email ID
msg.To.Add(sendTo); // adding the Recipient Email ID
if (!string.IsNullOrEmpty(carbonCopy)) // add CC email ids if supplied.
msg.CC.Add(carbonCopy);
msg.Subject = subject; //setting email subject and body
msg.Body = body;
msg.Attachments.Add(image);
//create a Smtp Mail which will automatically get the smtp server details
//from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Host = "smtp.gmail.com";
SmtpMail.Port = 587;
SmtpMail.EnableSsl = true;
SmtpMail.UseDefaultCredentials = false;
SmtpMail.Credentials = new System.Net.NetworkCredential(from, "password");
// sending the message.
try
{
SmtpMail.Send(msg);
}
catch (Exception ex) { }
}
More than likely your image SRC attribute is not an absolute, publicly-accessible URI. Any file-system or local URI's will not show the image in the email.
Specifically, these will not work:
c://test.png
test.png
/folder/test.png
http://localhost/test.png
http://internaldomain/test.png
Ensure that your image urls are
absolute (i.e. start with the protocol like http://)
includes a full path to the image
the domain is a public domain
Try the code part from this thread:
// creating the attachment
System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(#"c:\\test.png");
inline.ContentDisposition.Inline = true;
// sending the message
MailMessage email = new MailMessage();
// set the information of the message (subject, body ecc...)
// send the message
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
smtp.Send(email);
email.Dispose();
I'd be grateful if someone could tell me if I'm on the right track... Basically, I have a webservice i need to run for my app, I've put it into a try catch, if the try fails I want the catch to send me an email message with the details of the exception.
try
{
// run webservice here
}
catch (Exception ex)
{
string strTo = "scott#...";
string strFrom = "web#...";
string strSubject = "Webservice Not Run";
SmtpMail.SmtpServer = "thepostoffice";
SmtpMail.Send(strFrom, strTo, strSubject, ex.ToString());
}
Yes you are but you'd better wrapp yor exception handler in some kind of logger or use existing ones like Log4Net or NLog.
A quick and easy way to send emails whenever an Exception occurs can be done like this:
SmtpClient Server = new SmtpClient();
Server.Host = ""; //example: smtp.gmail.com
Server.Port = ; //example: 587 if you're using gmail
Server.EnableSsl = true; //If the smtp server requires it
//Server Credentials
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "youremail#gmail.com";
credentials.Password = "your password here";
//assign the credential details to server
Server.Credentials = credentials;
//create sender's address
MailAddress sender = new MailAddress("Sender email address", "sender name");
//create receiver's address
MailAddress receiver = new MailAddress("receiver email address", "receiver name");
MailMessage message = new MailMessage(sender, receiver);
message.Subject = "Your Subject";
message.Body = ex.ToString();
//send the email
Server.Send(message);
Yes this the right way, if you don't want to use any logger tool.You can create function SendMail(string Exceptioin) to one of the your common class and than call this function from each catch block
May I know how to send email based on gridview column containing email addresses?
I am currently using asp.net and c#. I'm also currently using smtp gmail for the email.
Currently, I had a gridview1 which contain customers (email, name, accountNo) that had bounced cheque, however I wish to send an standard email, to all these customers upon clicking a button. May I know how should i go about it? Their email is stored in database and will be shown on gridview.
private void SendEMail(MailMessage mail)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("#gmail.com", "password");
try
{
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
The easiest would be loop through the dataset that you have bound to grid view. But since you have asked about gridview, here is how you can loop through gridview rows
On button_click write this
string email = "";
foreach (GridViewRow item in GridView1.Rows)
{
//considering 1st column contains email address
//if not, replace it with correct index
email = item.Cells[0].Text;
//code to send email
}
UPDATE 2
Updating my code to use your sendEmail function
public void button_click(object sender, EventArgs e)
{
string email = "";
foreach (GridViewRow item in GridView1.Rows)
{
//if not, replace it with correct index
email = item.Cells[4].Text;
//code to send email
//reciever email add
MailAddress to = new MailAddress(email);
//sender email address
MailAddress from = new MailAddress("your#email");
MailMessage msg = new MailMessage();
//use reason shown in grid
msg.Subject = item.Cells[3].Text;
//you can similar extract FName, LName, Account number from grid
// and use it in your message body
//Keep your message body like
str email_msg = "Dear {0} {1}, Your cheque for account number {2} bounced because of the reason {3}";
msg = String.Format(email_msg , item.Cells[0].Text,item.Cells[1].Text,item.Cells[2].Text,item.Cells[3].Text);
msg.Body = email_msg ;
msg.From = from;
msg.To.Add(to);
SendEMail(msg);
}
}
try to save the e-mail addresses in an array.
then do a foreach and send the e-mail that are in the array!
example:
CLASS:
public SmtpClient client = new SmtpClient();
public MailMessage msg = new MailMessage();
public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("mail", "password");
public void Send(string sendTo, string sendFrom, string subject, string body)
{
try
{
//setup SMTP Host Here
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = smtpCreds;
client.EnableSsl = true;
//convert string to MailAdress
MailAddress to = new MailAddress(sendTo);
MailAddress from = new MailAddress(sendFrom);
//set up message settings
msg.Subject = subject;
msg.Body = body;
msg.From = from;
msg.To.Add(to);
// Send E-mail
client.Send(msg);
}
catch (Exception error)
{
}
}
SENDING E-MAILS: (button_click)
//read the emails from the database and save them in an array.
//count how many are the emails, ex: int countEmails = SqlClass.Function("select emails from table").Rows.Count;
string[] emails = new string[countEmails];
foreach (string item in emails)
{
//send e-mail
callClass.Send(item, emailFrom, subject, body); //you can adapt the class
}
The following function works perfectly:
protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo,
string To, string Subject, string Body, string Attachments)
{
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",
"smtpout.secureserver.net");
msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",
2);
msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendusername", From);
msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendpassword",
mailpassword);
msg.To = To;
msg.From = DisplayName + "<" + From + ">";
msg.BodyFormat = MailFormat.Html;
msg.Subject = Subject;
msg.Body = Body;
msg.Headers.Add("Reply-To", ReplyTo);
SmtpMail.SmtpServer = "smtpout.secureserver.net";
SmtpMail.Send(msg);
}
However, I get a build warning telling me that System.Web.Mail is obsolete, and
that I should be using System.Net.Mail. So I used System.Net.Mail, and I came up with
the following function to replace my old one:
protected void SendNetMailMessage(string DisplayName, string From, string ReplyTo,
string To, string Subject, string Body, string Attachments)
{
MailAddress addrfrom = new MailAddress(From, DisplayName);
MailAddress addrto = new MailAddress(To);
MailAddress replytoaddr = new MailAddress(ReplyTo);
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = addrfrom;
msg.To.Add(addrto);
msg.ReplyTo = replytoaddr;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtpout.secureserver.net");
smtp.Credentials = new NetworkCredential(From, mailpassword);
smtp.Send(msg);
}
I don't get any exceptions or errors, but my message never goes through. Can anyone
tell me what I might be doing wrong? Thanks in advance.
If I'm not wrong... you are using the Goddady hosting.
I'm also using it... and I have **relay-hosting.secureserver.net** as the SMTP server.
I don't know... may be can be something like that... or you have not set the Relay in your console.
I tested you code above an it works fine. I had to add the body and subject properties, but even without them I still received a blank email. I also tried with and without providing credentials, both worked.
Initially I hadn't changed your host string and did receive an exception with the message "Failure sending mail."