Programatically sending an email takes long, way to accelerate? - c#

In my current WPF project I built in a button, which sends emails to all elements in an array (these elements are persons, which all have an email address referring to).
I use System.Net.Mail for this.
Now, the problem is that it takes the button very long to send all these emails, even with one email sent it takes about a second to send. So the program is for this certain time not usable, you have to wait until all emails have been sent. This interrupts everything of course and is not wished.
Is there a way to accelerate this process?
This is my used code for sending the email:
string[] myArray = myList.ToArray();
foreach (string str in myArray)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myMail#Adress.com", "myEmailName");
mail.To.Add(targetEmailAdress);
mail.Subject ="mySubject";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody =
"<html> " +
"<body>" +
"<p>MyText123</p>" +
"</body> " +
"</html>";
mail.Body = htmlBody;
SmtpServer.Port = myPort;
SmtpServer.Credentials = new System.Net.NetworkCredential("myMail#Adress.com", #"myPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}

Related

Send multiple emails from listbox c#

hi im geting my email list from db in a listbox(email_c) and i need to send to all
this is the error that i get with my code
System.FormatException: 'The specified string is not in the form required for an e-mail address.'
login = new NetworkCredential(txtuser.Text, txtPassword.Text);
client1 = new SmtpClient(txtSmtp.Text);
client1.Port = Convert.ToInt32(txtPort.Text);
client1.Credentials = login;
string emails = email_c.GetItemText(email_c.Items);
msg = new MailMessage { From = new MailAddress(txtuser.Text + txtSmtp.Text.Replace("smtp.", "#"), "Sesizare", Encoding.UTF8) };
msg.To.Add(new MailAddress(emails));
if (!string.IsNullOrEmpty(txtCC.Text))
msg.To.Add(new MailAddress(txtCC.Text));
msg.Subject = txtSubject.Text;
msg.Body = "Abonamentul dumneavoastra a fost activat";
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client1.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "Ttimite...";
client1.SendAsync(msg, userstate);
The MailAddress does represent the address of an electronic mail sender or recipient. That means it is one single email.
Because of that, instead of writing all the email string into a single string variable you should iterate over the items in the listbox and add each of them individually to the MailMessage. Something like this:
foreach (var email in email_c.Items)
{
msg.To.Add(new MailAddress(email.ToString()));
}

How to send an email from any Email Address in ASP.Net/C# Webforms

I am a beginner programmer in C#, I want to create a contact form , where the user can enter any email address as the "From" address, and they can send a message. Every time it sends, it says the "From" address is the credentials that I am using. How do I change the From address to the address based on the user's input like any other contact form. Should I use a different Smtp Server?
Thanks
protected void sendbttn_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt.Text;
message.Body = mesgtxt.Text;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("Credemail", "Credpassword");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");
fromtxt.Text = "";
subjecttxt.Text = "";
mesgtxt.Text = "";
}
catch
{
MessageBox.Show("Something went wrong, please try again");
}
If your host is not registered as official mail provider, you should never even think about sending E-Mail in the name of somebody else.
Instead use a dedicated address as sender and to authenticate at the smart host and remember to store the credentials in a save location so nobody can "adopt" them - the program sourcecode as in the example is definitely not a save place, store them in IIS property fields.
The address entered in the form may be used as reply-to and possibly as the senders display name.
The body will not be html, too. The property therefore should be 'false'.
string credMail = "example#gmail.com";
string credPasswd = "example_p#asswd";
MailMessage message = new MailMessage();
message.From = new MailAddress(credMail,fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt;
message.Body = mesgtxt.Text;
message.IsBodyHtml = false;
message.ReplyToList.Add(fromtxt.Text);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(credMail, credPasswd);
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");

I want to email the screenshot in tabular format stored in my local using C#?

I want to automate my work by sending the screenshot taken during automation in tabular email format . I had successfully attached as document, but I want it in tabular format. This is code what I had already tried
public static void email_send() {
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:test1.jpeg\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource("test1.jpeg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);
Attachment att = new Attachment(#"D:/test123.png");
att.ContentDisposition.Inline = true;
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
System.Net.Mail.SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("*****");
mail.To.Add("*******");
mail.Subject = "Test Mail - 1";
mail.Body = String.Format(
"<h3>Client: " + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("******", "******");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
You have missed the attachment.
Your linked resource also needs to be attached to the email. You need to add the following code:
mail.Attachments.Add(att);

Hyperlinks in mails not showing in gmail, but it does in Outlook

I've got a c# process that send emails to customers with a hyperlink in the mail. The mail is send from a SQL Server stored proc. My c# program just invokes the sp.
The hyperlink works fine in Outlook, but on online gmail it only shows as text. It is not clickable.
My mail text looks something like:
Hi.
This is the hyperlink:<br>
<a href=\"serveraddress\Documents\\123_128635312685687531322.gif\">
Click Here</a><br><br>
What should I do to fix it?
EDIT:
My code:
string email = "xx#gmail.com;
string password = "MyPassword";
var credentials = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email, senderName);
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
EDIT 2:
Compile message being send:
string message = #"Hi. <br>
This is the intro line in the mail message.<br>";
using (DataTable dtLinks = data.ExecuteDataSet(#"SELECT *
FROM LessonFiles
WHERE Course = " + dr["Course"].ToString().DBValue() + #" AND
Lesson = " + dr["NextLesson"].ToString().DBValue()).Tables[0])
{
int i = 0;
foreach (DataRow drLink in dtLinks.Rows)
{
i += 1;
message += "<a href=\"" + drLink["Link"].ToString() + "\">" + drLink["Lesson"].ToString();
message += i == 1 ? "" : " file " + i;
message += "</a>" + "<br>";
}
}
message += "<br>Regards<br><br>";
try to add target="_blank", like this...
message += "<a href=\"" + drLink["Link"].ToString() + "\"target=\"_blank\">" + drLink["Lesson"].ToString();
Seems like something was funny with the hyperlink itself.
Using http://serveraddress/Documents/logoColourBG635315550177822533.jpg seems to work.
The original contained backslashes in the path. The fact that it showed the hyperlink in Outlook led me to believe the address was correct.
Thanks for all your help.
creating mail message object...
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(UserName, Password);
smtp.Timeout = 20000;
MailMessage Msg = new MailMessage();
Msg.IsBodyHtml = true;
MailAddress fromMail = new MailAddress(SenderID);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(TosendID));
Msg.Subject = subject;
Msg.Body = body;
In body add ur code.....
Hope This Helps.........

Progress status of file upload in smtpClient [duplicate]

I am using this function to send mails via gmail.
private bool uploadToGmail(string username, string password , string file ,
string backupnumber)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("jain#gmail.com");
mail.To.Add("jain#gmail.com");
mail.Subject = "Backup mail- Dated- " + DateTime.Now + " part - " +
backupnumber;
mail.Body = "Hi self. This mail contains \n backup number- " +
backupnumber + " \n Dated- " + DateTime.Now ;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(file);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials =
new System.Net.NetworkCredential("jain#gmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 999999999;
SmtpServer.Send(mail);
// MessageBox.Show("mail Sent");
return true;
}
Now I want to show a progress bar (in case there is a large attachment) to show the upload . Is this possible ? I think I know how to use a progress bar, but don't know how to use it with Smtpclient.send() .
any help ?
Thanks
You should use SendAsync and subscribe to SendCompleted, to know, when the sending your mail completed. There is no way to get the progress of the send process, though...

Categories

Resources