Progress status of file upload in smtpClient [duplicate] - c#

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...

Related

Programatically sending an email takes long, way to accelerate?

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);
}

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);

Sending Email with attachment without saving the attachment c# / ASP.NET

I am trying to send an email with an attachment that will be uploaded by the user and then sent to an admin email.
I have got this configured correctly on IE 11 but however with Chrome/Firefox there is limitations with the FilePath it provides. As IE 11 provides the full file path it allows my function to work.
Is there a possible way round this for Chrome/Firefox.
Mail Message code:
protected void Submit_Click(object sender, EventArgs e)
{
using (MailMessage message = new MailMessage())
{
if (Attachment1.HasFile == false)
{
message.From = new MailAddress(Environment.UserName + "#domain");
message.To.Add(new MailAddress("MyEmail"));
message.IsBodyHtml = true;
message.Subject = "New Request from self service portal: " + Summary.Text.ToString();
message.Body = "Customer Name:</br>Customer Username:" + Environment.UserName + "</br>" + DetailedSummary.Text.ToString();
SmtpClient client = new SmtpClient();
client.Host = "IP ADDRESS";
client.Send(message);
} else {
message.From = new MailAddress(Environment.UserName + "#domain");
message.To.Add(new MailAddress("myemail"));
string file = Attachment1.PostedFile.FileName;
message.Attachments.Add(new Attachment(file));
message.IsBodyHtml = true;
message.Subject = "New Request from self service portal: " + Summary.Text.ToString();
message.Body = "Customer Name:</br>Customer Username:" + Environment.UserName + "</br>" + DetailedSummary.Text.ToString();
SmtpClient client = new SmtpClient();
client.Host = "IP ADDRESS";
client.Send(message);
}
}
}
This is where the user will specify which file will be uploaded and will not be a static file being uploaded every time. Meaning, i will need the filepath from the FileUpload.
HttpPostedFile.FileName "Gets the fully qualified name of the file on the client."
During development on your machine (and using a browser that actually sends the full path, which proper browsers don't) that may work, but as soon as you deploy it on a server it'll break.
The easiest way would be to use the new Attachment(Attachment1.PostedFile.InputStream, "attachmentname") constructor to directly stream the uploaded file into your attachment without having to temporarily save it yourself.

C# mail is not displaying link in my email

I am trying to send email with unsubsrible link in my aspx page. But when I check my email id It display only text. There was no link. Here is my code generate a email
string bodyContent = CKEditor1.Text;
string userLink = "http://www.abc.in/Message.aspx?action=rmsb&oldsubuser=";
string footerLink = "</br></br></br>You are receiving this mail because you have subscribed to our newsletter. If you do not wish to receive the mail, Click <a href='" + userLink + "" + ids[i].ToString() + "'>Here</a>";
bodyContent = bodyContent + footerLink;
EmailSend newsletter = new EmailSend();
newsletter.NewsLetterSend(ids[i].ToString(), bodyContent.Replace("'", "''"), txtSubject.Text.Replace("'", "''"));
//EmailSend.SendMailMessage("faredpt#gmail.com", ids[i].ToString(), "", "", txtSubject.Text, bodyContent);
bodyContent = bodyContent.Replace(footerLink, " ");
Here is the code for NewsLetterSend function
public void NewsLetterSend(string getemailAdd, string msgBody, string subject)
{
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("admin#abc.in", "admin#abc.in");
mail.To.Add(getemailAdd.Trim());
//set the content
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = msgBody;
mail.Priority = MailPriority.High;
//set the smtp settings
SmtpClient smtp = new SmtpClient("abc.in");
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("admin#abc.in", "i#abc!23#");
//smtp.Port = 3535;
smtp.Port = 25;
//send email
smtp.Send(mail);
return;
}
Now this code is sending email successfully but unable to add link in my email. It show me simple text
Please tell me why this happening
You are replacing the quotation mark in href with two quotation marks!
Invalid statement: bodyContent.Replace("'", "''")
This will render your HTML invalid.
Could you try to replace </br></br></br> with <br/><br/><br/>? Probably because of invalid br tags your email is considered as not valid HTML and links become broken.

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.........

Categories

Resources