Send Email using SMTP in C# + Template Processing Mail Merge - c#

Currently i am using SMTP Client to send email in C#.
I have one email template defined in which i replace the value at run time.
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "~/Email-Templates/File.html";
mailDefinition.From = "abc#gmail.com";
//Create a key-value collection of all the tokens you want to replace in your template...
ListDictionary replacements = new ListDictionary();
replacements.Add("<%FirstName%>", "abc");
replacements.Add("<%LastName%>", "xyz");
replacements.Add("<%ManagerName%>", "Tom");
replacements.Add("<%Address1%>", "USA");
replacements.Add("<%Address2%>", "USA");
replacements.Add("<%City%>", "uk");
replacements.Add("<%State%>", "uk");
replacements.Add("<%Zip%>", "9876543");
string mailTo = string.Format("{0} {1} <{2}>", "abc", "xyz", "abc#gmail.com");
MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, replacements, this);
mailMessage.From = new MailAddress("abc#gmail.com", "test site");
mailMessage.IsBodyHtml = true;
mailMessage.Subject = "User Details";
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("abc#gmail.com", "aaaaaaaa"),
EnableSsl = true
};
client.Send(mailMessage);
Template looks like this :
Hello <%FirstName%> <%LastName%>,
Thank you for creating an account with us.
Here are your details:
Your Manager is <%ManagerName%>
<%Address1%>,
<%Address2%>
<%City%>, <%State%> <%Zip%>
Thank You,
abc
Now my question is in future i want to add newfield to the email template , i need to add the same to my code and rebuild the system, this is what i dont want , i dont want to build my system everytime.
Is there a way or some other approach where i need not to build the system everytime ?
I can configure this from some where else ?
Thanks.

Related

Can clients send email to each other via your website?

I am working on an online shopping website. I want the users directly contact each other via emails by using a web form. I have tried different ways. but it didn't work.
I was also trying to use the gmail smtp as follow:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("example#gmail.com", "mypassword");
client.EnableSsl = true;
string fromAddress = "buyer#gmail.com";
string messageSubject = "Your Subject";
string body = "Content";
var supEmail = "seller#yahoo.com";
MailMessage mm = new MailMessage(fromAddress, supEmail, messageSubject, body);
MailAddress copy = new MailAddress("notifications#mydomain.com");
mm.CC.Add(copy);
//Set up your encoding
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.Priority = System.Net.Mail.MailPriority.High;
//Send your message
client.Send(mm);
Any idea or suggestions?
This won't work. Google won't allow you to send an email from an account you don't own... so if you want to send an email via Google then it needs to be from either the Google email account your are validating with or associated with that account.

How to send group emails asp.net/C# depending on select query

I am trying to send a group email depending on emails gathered from a select query called emailresults.
This is the code I have got so far but I am receiving error:
Could not send the e-mail - error: The specified string is not in the form required for an e-mail address
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
message.To.Add(emailresult);
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If emailResults is some kind of Enumerable you will need to add each email address string from the Enumerable to the MailMessage.To MailAddressCollection. It's hard to say exactly how to do this without knowing the Type of emailresults. I expect you need something along these lines.
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
for(var item in emailresult){
message.To.Add(item);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If you can confirm what Type emailResults is it will be much easier to provide a clear solution.
In your comment above you state that emailresult is a select query result, so some sort of enumarable? Or as another poster suggested a long string separated by commas or semicolons?
Try doing one of these suggestions...
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
//For each item in the list, add it to the list of "To" recipients
emailresult.ForEach(r => message.To.Add(r)) ;
//OR IF THE ADDRESS IS A PROPERTY OF THE ITEM
emailresult.ForEach(r => message.To.Add(r.MyEmailAddressProperty)) ;
//OR IF emailresult is some sort of string list, "email1,email2,email3" or similar then
foreach (string oneEmail in emailresult.Split(","))
{
message.To.Add(oneEmail);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);

Sending email using CDO

I have a database which contains email address of recipients and a flag column. If the flag is "YES", a mail will be sent to that user. I want to accomplish this using CDO in my ASP.NET project. So, what may be the favorable routine for doing this. I am new to CDO.
What I tried as a beginner is this:
CDO.Message oMsg = new CDO.Message();
CDO.IConfiguration iConfg;
iConfg = oMsg.Configuration;
ADODB.Fields oFields;
oFields = iConfg.Fields;
ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
oFields.Update();
oMsg.Subject = "Test CDO";
oMsg.From = "abc#xyz.com";
oMsg.To = "someone#example.com";
//oMsg.TextBody = "CDO Mail test";
oMsg.HTMLBody = "CDO Mail test";
oMsg.Send();
Response.Write("Mail Sent");
But its not helping me what I want.

Mail doesn't work with multiple to addresses [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unable to send an email to multiple addresses/recipients using C#
I have used below code to send mail in script task
string MailFromName = "Admin";
System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
mailmsg.IsBodyHtml = true;
mailmsg.Subject = strMessageSubject;
mailmsg.Body = strMessageBody;
mailobj.Host = strSMTPServerName;
mailobj.Send(mailmsg);
It is working fine when I am using MailToEmail as "myaddress#myMail.com" i.e. for one email address
but this doesn't send any mail(also it dosen't fail) when I have multiple adress in to list
ex: "MyAdress#MyMail.com; MySecondAddress#MyMail.com"
How to resolve this?
EDIT New Code
string MailFromName = "Admin";
System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
mailmsg.IsBodyHtml = true;
mailmsg.Subject = strMessageSubject;
mailmsg.Body = strMessageBody;
foreach (string str in multipleToMsg)
{
mailmsg.To.Add(str);
}
mailobj.Host = strSMTPServerName;
mailobj.Send(mailmsg);
You've not shown how exactly you are adding the recipients. However to add multiple recipients you add to the "To" collection:
MailMessage message = new MailMessage();
message.To.Add("sillyjoe#stackoverflow.com");
"To" is a collection of MailAddresses. Make sure you are adding it to that collection and not attempting to concatenate email addresses all into one MailAddress object.
Accoring to MSDN: MailMessage Class the "To" property is a collection of MailAddresses
so you just need to do something like
mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail, MailToEmail));
mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail2, MailToEmail2))
or in a foreach loop
//get email addresses into a collection called emailAdds
foreach (var emailAdd in emailAdds)
{
mailmsg.To.Add(new System.Net.Mail.MailAddress(emailAdd, emailAdd ));
}
To specify multiple addresses you need to use the To property which is a MailAddressCollection
message.To.Add("one#example.com, one#example.com"));
message.To.Add("two#example.com, two#example.com"));

Sending Email through .NET code

I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008.
Please provide SMTP HostName, PortName for yahoo server and gmail server.
And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.
Please provide complete working code...so that i will copy into Visual studio environment and execute the same.
As i'm getting exception since morning....unable to resolve the issue.
Kindly help me in this regard.
For Gmail:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#gmail.com");
mail.To.Add("youraccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
For Yahoo:
var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("youraccount#yahoo.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#yahoo.com");
mail.To.Add("destaccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.
So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).
Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.
Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.
If you don't know how to enable logging for SNM, here is a link:
http://systemnetmail.com/faq/4.10.aspx
Thanks!
Dave
There are two ways in which We can send the mail,
1)First is using javascript link "mailTo".This will not send the mail automatically but it will just open the mail window.Refer to the below code
<a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a>
Find below the js method
function buildEmail(el) {
var emailId = Usermail#gmail.com;
var subject="Hi";
var body="Hello";
el.href = "mailto:" + emailId + "?Subject=" + escape(subject) +
"&Body=" + escape(body);
}
2)The second way is to use System.Net.Mail which will automatically send the mail to the recipients in the secured manner.
string subject="Hello";
string body="Data";
using ( MailMessage objMail = new MailMessage ( "Yourmail#gmail.com", "Usermail#gmail.com" ) )//From and To address respectively
{
objMail.IsBodyHtml = false;// Message format is plain text
objMail.Priority = MailPriority.High;// Mail Priority = High
objMail.Body = "Hello";
ArrayList CCarr = new ArrayList();//Assume we add recipients here
// populate additional recipients if specified
if ( ( CCarr != null ) && ( CCarr .Count > 0 ) )
{
foreach ( string recipient in CCarr )
{
if ( recipient != "Please update the email address" )
{
objMail.CC.Add ( new MailAddress ( recipient ) );
}
}
}
// Set the subject of the message - and make sure it is CIS Compliant
if ( !subject.StartsWith ( "SigabaSecure:" ) )
{
subject = "SigabaSecure: " + subject;
}
objMail.Subject = subject;
// setup credentials for the smpthost
string username = "Username";
string passwd = "xxxxxx";
string smtpHost = "mail.bankofamerica.com";
SmtpClient ss = new SmtpClient ();
ss.EnableSsl= true;
ss.Host = smtpHost;
ss.Credentials = new NetworkCredential ( username, passwd );
ss.Send ( objMail );
}
Sigaba Secure Email secures e-mail from client to client through the use of desktop plug-ins and Web-based authentication and decryption.

Categories