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);
Related
I have project related school management system in c# using asp.net as framework and sql server as database.
I need to send email with same email body and subject (like: wish you a very happy new year) but i have different from addresses and obviously different to addresses.
For example: i need to send email to all teachers with aa#aa.aa email and to all students with bb#bb.bb email address and to management staff with cc#cc.cc
How can i perform this task in c# and asp.net with efficient way?
You can create SmtpClient and MailMessage object in order to send email.
I think you should call your mail sending method with 3 times with different parameters (from address and receivers list) Your methos looks like
SendMail("aa#aa.aa", teacherList);
SendMail("bb#bb.bb", studentList);
SendMail("cc#cc.cc", staffList);
public static void SendMail(string fromAddress, List<string> emailAddresses)
{
//make this smtpClient global. Because you will use next time.
SmtpClient smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential("smtpUserName", "smtpPassword");
smtpClient.Host = "mail.mailhost.com";//set your smtp host. Generally mail.domain.com
smtpClient.Port = 587;//set your smtp port
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;//you can change this based on your settings
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromAddress, "Our School Name");
for (int i = 0; i < emaillAddresses.Count; i++)
{
mail.Bcc.Add(new MailAddress(emaillAddresses[i]));
}
mail.Subject = "Wish you a very happy new year";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.UTF8;
smtpClient.Send(mail);
}
I'm new to C# and am trying to get a list of files from a directory and then send them in an e-mail. I can do both things individually, but just can't seem to work out. Here is my basic code to get a list of files:
foreach (string str in Directory.GetFiles(path))
{
Message.Print(str);
}
For my e-mail code, I have this:
SmtpClient smtpClient = new SmtpClient(server, Port);
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
smtpClient.EnableSsl = ssl;
MailAddress fromAddress = new MailAddress(sender);
MailMessage message = new MailMessage();
message.From = fromAddress;
message.Subject = "Test e-mail";
message.IsBodyHtml = false;
message.Body = "List directory content here";
message.To.Add(reciever);
smtpClient.Send(message);
No matter what I try, I just can't seem to work out how to list the directory contents in the e-mail body. Can anyone assist?
Directory.GetFiles(path) is an array, you can use string.Join to get an string out of that instead of your current foreach loop, then you just use the resulting string for message.Body.
message.Body = sting.Join(",", Directory.GetFiles(path))
This is the initial step to get it working, validations need to be done in order to make this production ready. Check Directory.GetFiles exceptions to get an idea of all the stuff that can go wrong with this code.
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"));
I was trying to change FROM field with Name and Email address. But When I receive the email, Its shown as below
My network <commity#company.com [My network <commity#company.com]
Mycode is like below
const string from = "My network <commity#company.com>";
StringDictionary headers = new StringDictionary();
headers.Add("to", invitee.userEmail);
headers.Add("subject", ltEmailSubject.Text);
headers.Add("from", from);
headers.Add("content-type", MediaTypeNames.Text.Html);
_emailSuccessful = SPUtility.SendEmail(elevatedSite.RootWeb, headers,
emailBody + Environment.NewLine + "");
I want FROM email address should showup like below
My network [commity#company.com]
SPUtility.SendMail will always use the from smtp address as specified in Central Administration > Outgoing Email Settings and the friendly name is set to the title of the site.
Instead you can use (from http://www.systemnetmail.com/faq/3.2.1.aspx)
MailMessage mail = new MailMessage();
//set the addresses
//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress("me#mycompany.com", "Steve James" );
mail.To.Add("you#yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
I know this thread is old, but in case someone comes across this as I did, your original code would have worked fine using SPUtility.SendMail if you added quotes around the friendly name in the from address like this:
const string from = "\"My network\" <commity#company.com>";
I believe if you look at the mail object in the .Net Framework, you could do what you wish to do.
Maybe this site could help you out further?
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.