MailMessage sends mail with empty subject and attachments - c#

So I'm trying to send an email through my relay smtp with an html body, subject and optional attachments. The sending works without exceptions however, the mail which is being sent ends up with an empty subject and no attachments which should get files from the wwwroot folder of my web application which is being hosted on the same domain as my API and console app. This console app will be called from the task scheduler. This program has my API as dependency so that it can call the ProcessQueue task. this one is being called correctly from my console application. To get data for the email the code will retreive data from the MailQueue table and then fill the mailmessage like so:
public async Task ProcessQueue(int range, bool send)
{
SmtpClient client = new SmtpClient
{
Host = "outbound.domain",
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("email#domain.com", "")
};
var items = await _context.MailQueues.Take(range).ToListAsync();
MailMessage message = new MailMessage();
foreach (var item in items)
{
try
{
message.From = new MailAddress(item.From);
message.Subject = item.Subject; // The subject is being filled correctly in my situation, but when the email arrives it isn't
message.IsBodyHtml = true;
message.To.Add(item.To);
message.Body = item.Content;
if (send)
{
AddAttachments(message, item.Docs);
client.Send(message);
_context.MailQueues.Remove(item);
message.To.Clear();
}
}
catch(Exception ex)
{
item.Exception = ex.ToString();
_context.Entry(item).State = System.Data.Entity.EntityState.Modified;
}
}
The attachment method:
private void AddAttachments(MailMessage message, string docs)
{
if (docs != null)
{
List<string> list = JsonConvert.DeserializeObject<List<string>>(docs);
foreach (string item2 in list)
{
Attachment item = new Attachment(HttpContext.Current.Server.MapPath("/wwwroot/documents/") + item2)
{
Name = item2
};
message.Attachments.Add(item);
}
}
}
The structure of my domain:
domain.com > webapplicatie (contains my web app and its wwwroot folder)
domain.com > webapi (contains my web api)
domain.com > mailqueuer (the location of my console application)
My goal is to send the message with optional attachments, which are located in the web app's wwwroot folder, and its subject. All the data of the MailQueue objects are filled! but still I get this problem.
Does somebody know a solution to this?

I solved this problem by putting the database and smtp logic for the MailQueue into the console application. I don't exactly know why my question isn't working but I'm open for answers and suggestions!

Related

How do I call a method to automatically send an email in the controller?

I need to be able to send automatic emails to any users who have registered new accounts, changed passwords, and/or created new orders.
I've been given the SendEmail file, which belongs in the "Utilities" folder in my solution.
using System;
using System.Net.Mail;
using System.Net;
namespace SendEmail
{
public static class EmailMessaging
{
public static void SendEmail(String toEmailAddress, String emailSubject, String emailBody)
{
//Create a variable for YOUR TEAM'S Email address
//This is the address that will be SENDING the emails (the FROM address)
String strFromEmailAddress = "email#gmail.com";
//This is the password for YOUR TEAM'S "fake" Gmail account
String strPassword = "Password";
//This is the name of the business from which you are sending
//TODO: Change this to the name of the company you are creating the website for
String strCompanyName = "Team Final Project";
//Create an email client to send the emails
//port 587 is required to work, do not change it
var client = new SmtpClient("smtp.gmail.com", 587)
{
UseDefaultCredentials = false,
//This is the SENDING email address and password
//This will be your team's email address and password
Credentials = new NetworkCredential(strFromEmailAddress, strPassword),
EnableSsl = true
};
//Add anything that you need to the body of the message
//emailBody is passed into the method as a parameter
// /n is a new line – this will add some white space after the main body of the message
//TODO: Change or remove the disclaimer below
String finalMessage = emailBody + "\n\n Thank you, come back again soon!";
//Create an email address object for the sender address
MailAddress senderEmail = new MailAddress(strFromEmailAddress, strCompanyName);
//Create a new mail message
MailMessage mm = new MailMessage();
//Set the subject line of the message (including your team number)
mm.Subject = "Team ## - " + "Thank you!";
//Set the sender address
mm.Sender = senderEmail;
//Set the from address
mm.From = senderEmail;
//Add the recipient (passed in as a parameter) to the list of people receiving the email
mm.To.Add(new MailAddress(toEmailAddress));
//Add the message (passed)
mm.Body = finalMessage;
//send the message!
client.Send(mm);
}
}
}
My problem is that neither I nor my team members know how to implement call this from the controller in a way that will be sent automatically and with the user's email and name. We imagine they will be in the Account and Orders controllers. The accounts controller has the register and change password methods, which work, and the orders controller has the complete order method.
Also, we are not using a confirmation view, it has to be an automatic email.
We need some direction in figuring out where exactly we need to call the method from and how.
The most helpful thing I've found on the internet today is this block of code for a test message that is not intended to be sending automatic emails.
public static void CreateTestMessage(string server)
{
MailAddress from = new MailAddress("sender#gmail.com", "Team Project");
MailAddress to = new MailAddress("reciever#gmail.com", "Customer");
MailMessage message = new MailMessage(from, to);
message.Subject = "Password Changed";
message.Body = #"This is a confirmation email that your password has been changed.";
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
Everything is being coded on MS VS
First create a service for EmailService and put SendEmailAsync Method in it
and call this method on your Controller.
In Method body:
1-Create your message:
var mm = new MailMessage()
2-Then you should build your smtp:
using var smtpClient = new SmtpClient();
3-Then connect it to your server
await smtpClient.ConnectAsync(
"your host",
"port",
SecureSocketOptions.SslOnConnect);
4-Now send your Email:
await smtpClient.SendAsync(mm);
5-Make sure that disconnect from your client:
await smtpClient.DisconnectAsync(true);
Note: It may give you an Exception while Connecting or Sending your Email so don't forget try catch block.
6-For Automation you can use a EmailAccount Table with relation with your Customer Table and keep your message data in it.
for ex: body ,subject..... .
In your Contorller you have your Customer So you can get his EmailAccount from DataBase and pass EmailAccount Entity to SendEmailAsync Method.
instead of creating message in body, get it from EmailAccount Entity and then countinue the Steps.
7-Enjoy it :)

Send email with previous messages by mailkit (smtp)

My application is windows service that send and get messages from mailbox. Emails are saved in the SQL Server database. Now my application send actual text/html (previous emails text is not included in sending message). I want to add possibility to send email with previous emails text/html. Does mailkit or smtp have feature which will help me to implement this mechanism or I should I collect data about every email and construct text/html by myself.
I want to send emails this way
Now I send emails this way
Here is my example of sending email:
var mimeMessage = new MimeMessage();
var multipart = new Multipart("mixed");
mimeMessage.To.AddRange(GetListOfMailboxAddresses(row.Field<int?>("ToGroup")) ?? new List<MailboxAddress>());
mimeMessage.Cc.AddRange(GetListOfMailboxAddresses(row.Field<int?>("CcGroup")) ?? new List<MailboxAddress>());
mimeMessage.Bcc.AddRange(GetListOfMailboxAddresses(row.Field<int?>("BccGroup")) ?? new List<MailboxAddress>());
mimeMessage.From.Add(new MailboxAddress(row["FromName"].ToString(), PluginHelper.ConfigurationXML.SmtpKonfig.SmtpUzytkownik));
mimeMessage.Sender = new MailboxAddress(row["FromName"].ToString(), PluginHelper.ConfigurationXML.SmtpKonfig.SmtpUzytkownik);
mimeMessage.Subject = row["Subject"].ToString();
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = row["HTML"].ToString();
bodyBuilder.TextBody = row["Text"].ToString();
multipart.Add(bodyBuilder.ToMessageBody());
var attachment = new MimePart();
if (Convert.ToInt32(row["SendingEmailId"]) == (int)EmailSendingType.Reply)
{
if (!mimeMessage.Subject.StartsWith("Re:", StringComparison.OrdinalIgnoreCase))
mimeMessage.Subject = "Re:" + mimeMessage.Subject;
ConstructReplyReferences(DataAccess.GetGuidsForReply(Convert.ToInt32(row["ConversationId"]), Convert.ToInt32(row["Id"])), row["ReplyToGuid"].ToString(), mimeMessage);
try
{
ConstructReplyReferencesFromCREW(row.Field<int?>("ObjectNumber"), mimeMessage);
}
catch (Exception ex)
{
LoggerHelper.LogInfo(ex.ToString());
}
}
if (!string.IsNullOrWhiteSpace(row.Field<string>("ReplyEmailInUserWindow")))
{
mimeMessage.ReplyTo.Clear();
mimeMessage.ReplyTo.Add(new MailboxAddress(row.Field<string>("ReplyEmailInUserWindow")));
}
GetAddAttachments(multipart, Convert.ToInt32(row["Id"]));
mimeMessage.Body = multipart;
client.Send(mimeMessage);

Error when sending both an email and a fax with the same SMTP (C#)

I am currently working on a routine that automatically generates a Text from some predefined Settings, and should send that Text either as an Email or a Fax - or even both.
Code looks like this:
SmtpClient smtp = new SmtpClient(Properties.Settings.Default.SmtpServer)
{
UseDefaultCredentials = true
};
if (IsMail)
{
var sendMail = "current users email";
using (MailMessage mm = new MailMessage(sendMail, this.EmailAddress))
{
mm.Subject = this.Subject;
if (!string.IsNullOrEmpty(this.MailBody))
{
mm.Body = this.MailBody.Replace("\n\n", "<br><br>");
mm.IsBodyHtml = true;
}
if (this.IsCcMail)
mm.CC.Add(sendMail);
foreach (var a in Attachments)
mm.Attachments.Add(a);
smtp.Send(mm);
}
}
if (IsFax)
{
var formatedFaxNo = this.CountryPhoneCode + FaxNo.TrimStart('0');
using (MailMessage mm = new MailMessage("current users email", formatedFaxNo + "#fax"))
{
mm.Subject = this.Subject;
if (!string.IsNullOrEmpty(this.MailBody))
{
mm.Body = this.MailBody.Replace("\n\n", "<br><br>");
mm.IsBodyHtml = true;
}
if (IsCcFax)
mm.CC.Add("current users fax" + "#fax");
if (ShowMessageYesNo(Translator.WouldYouLikeToSendTheFaxWithACoverSheet) != System.Windows.MessageBoxResult.Yes)
mm.Headers.Add("EMPTYBODYTEXT", "1");
else
{
var faxBody = Properties.Resources.FaxTemplate.ToString();
mm.Body = string.Format(faxBody,
Translator.Fax, formatedFaxNo,
Translator.From, "current users name",
Translator.Subject, this.Subject,
Translator.Date, DateTime.Now.ToShortDateString(),
this.MailBody?.Replace("\n\n","<br><br>"));
}
foreach (var a in Attachments)
mm.Attachments.Add(a);
smtp.Send(mm);
}
}
If I want to send both an email and a Fax, I get a SMTP exception at the time the Fax is supposed to be sent - the Email is successfully sent before.
The Error only reads
"Error sending Mail"
Which is not really helpful.
So far I've tried:
Sending two mails instead of a Mail and a Fax - works as intended;
Sending two Faxes likewise - works as intended;
Changing the Order and sending the Fax before the Mail - The Fax gets sent, but I get the SMTP exception on sending the mail;
Disposing the SmtpClient after sending the Mail and creating a new one for the Fax - does not change anything;
To me it seems like there is an issue with the SMTP Server I'm trying to send stuff from, but I'm currently not able to change anything about the Server.
Does anyone here have any further suggestions what I could try to fix that Issue?

Doubts on sending more than one email asynchronously in MVC3

In my application I have a functionality to save and publish articles. So when I click on "Save and Publish" button three things happened:
Published articles get saved in database.
A Notification email goes to a group of users that a new articles is available.
After sending emails page get redirect to "Article Listing" page without showing any success or failure message for an email.
Now Number of users who will receive emails can vary say for e.g 10, 30 50 and so on. I want to send notification emails asynchronously so that page won't get block until all the mails doesn't go to their receptionists.
Given below is a piece of code from "PublishArticle" action method
foreach (string to in emailIds)
{
ArticleNotificationDelegate proc = Email.Send;
IAsyncResult asyncResult = proc.BeginInvoke(subject, body, to, from, cc, null, null, null);
}
Below I have defined a delegate to invoke Send method
private delegate bool ArticleNotificationDelegate (string subject, string body, string to, string from, string cc, string bcc = null);
and this is the code to send an email:
public static bool Send(string subject, string body, string to, string from, string cc, string bcc = null)
{
bool response;
MailMessage mail = new MailMessage();
MailAddress fromAddress = new MailAddress(from);
mail.To.Add(to);
if (!string.IsNullOrWhiteSpace(cc))
{
mail.CC.Add(cc);
}
if (!string.IsNullOrWhiteSpace(bcc))
{
mail.Bcc.Add(bcc);
}
mail.From = fromAddress;
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
try
{
client.Send(mail);
response = true;
}
catch (Exception)
{
response = false;
}
finally
{
client.Dispose();
mail.Dispose();
}
return response;
}
Although this code is working fine but still I want to know that whether my this approach is fine and will not cause any problem in future.
If there is a better approach to accomplish my objective then please suggest me.
Note: As I am using .net framework 4.0 so cannot use the new features of Asyn and await available in 4.5.
Also I had used method client.SendAsync by simply replacing the client.Send and rest of my code in above Send method was same. After this change NO mails were being send and also it did not throw any exception. So I did not go with this change.
I want to send notification emails asynchronously so that page won't get block until all the mails doesn't go to their receptionists.
That's a very dangerous approach, because ASP.NET will feel free to tear down your app domain if there are no active requests. ASP.NET doesn't know that you've queued work to its thread pool (via BeginInvoke). I recommend that you use a solution like HangFire to reliably send email.

How to send email [duplicate]

This question already has answers here:
Sending email through Gmail SMTP server with C#
(31 answers)
Closed 9 years ago.
I am New in Asp.net, i need to send email from Asp.net using my Outlook.
I have one button in asp and when i click button(send) i want to send email.
I tried to use Hotmail and Gmail but remote server in blocked.
If you don't understand my question please tell me.
I tried this:
var smtpClient = new SmtpClient
{
Host = "outlook.mycompany.local",
UseDefaultCredentials = false,
Credentials = new NetworkCredential("myEmail#mycommpany.com", "myPassword")
};
var message = new System.Net.Mail.MailMessage
{
Subject = "Test Subject",
Body = "FOLLOW THE WHITE RABBIT",
IsBodyHtml = true,
From = new MailAddress("myemail#mycommapny.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("friendEmail#Company.com"));
// and here you're actually sending the message
smtpClient.Send(message);
}
Exeption Show: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
Please how can i do that ?
Sending outbound email from an ASP.net web site can be problematic. Even if you get the SMTP information right, you still have to deal with:
Sender Policy Framework (SPF)
Whitelists/Blacklists
Validation
Bouncebacks
It's very difficult to do this yourself, which is why you might want to consider using a service provider instead. You simply use their API (often a REST call), and they do the rest. Here are three such providers:
SendGrid
Mandrill
Mailgun
Mandrill has a low-end free plan, and so does SendGrid if you are using it with Windows Azure. And they are all reasonably affordable, even for the larger plans.
I highly recommend using one of these with their own API instead of using System.Net.Mail yourself. But if you want, they also can act as an SMTP relay for you so you can use their SMTP servers and keep your System.Net.Mail code intact.
First of all get the company SMTP server settings (from your sys admins I guess), then you can do something like this:
// setting up the server
var smtpClient = new SmtpClient
{
Host = "your.company.smtp.server",
UseDefaultCredentials = false,
EnableSsl = true, // <-- see if you need this
Credentials = new NetworkCredential("account_to_use", "password")
};
var message = new MailMessage
{
Subject = "Test Subject",
Body = "FOLLOW THE WHITE RABBIT",
IsBodyHtml = true,
From = new MailAddress("from#company.com")
};
// you can add multiple email addresses here
message.To.Add(new MailAddress("neo#matrix.com"));
// and here you're actually sending the message
smtpClient.Send(message);
you can use this function. and one thing you have to store you email smtp login and password in web config file
/// <summary>
/// Send Email
/// </summary>
/// <param name="strFrom"></param>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strAttachmentPath"></param>
/// <param name="IsBodyHTML"></param>
/// <returns></returns>
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
Array arrToArray;
char[] splitter = { ';' };
arrToArray = strTo.Split(splitter);
MailMessage mm = new MailMessage();
mm.From = new MailAddress(strFrom);
mm.Subject = strSubject;
mm.Body = strBody;
mm.IsBodyHtml = IsBodyHTML;
//mm.ReplyTo = new MailAddress("replyto#xyz.com");
foreach (string s in arrToArray)
{
mm.To.Add(new MailAddress(s));
}
if (strAttachmentPath != "")
{
try
{
//Add Attachment
Attachment attachFile = new Attachment(strAttachmentPath);
mm.Attachments.Add(attachFile);
}
catch { }
}
SmtpClient smtp = new SmtpClient();
try
{
smtp.Host = ConfigurationManager.AppSettings["MailServer"].ToString();
smtp.EnableSsl = true; //Depending on server SSL Settings true/false
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = ConfigurationManager.AppSettings["MailUserName"].ToString();
NetworkCred.Password = ConfigurationManager.AppSettings["MailPassword"].ToString();
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;//Specify your port No;
smtp.Send(mm);
return true;
}
catch
{
mm.Dispose();
smtp = null;
return false;
}
}
Try Amazon Simple Email Service (http://aws.amazon.com/ses/). If you're new to Amazon Web Services (AWS) there might be a learning curve. However, once you're familiar with their SDK which can be found on Nuget (AWSSDK) the process is very straight-forward (Amazon does have a lot of little wrapper classes which can be quirky).
So, to answer the question "How to send email?", it looks something like:
var fromAddress = "from#youraddress.com";
var toAddresses = new Amazon.SimpleEmail.Model.Destination("someone#somedestination.com");
var subject = new Amazon.SimpleEmail.Model.Content("Message");
var body= new Body(new Amazon.SimpleEmail.Model.Content("Body"));
var message = new Message(subject , body);
var client = ConfigUtility.AmazonSimpleEmailServiceClient;
var request= new Amazon.SimpleEmail.Model.SendEmailRequest();
request.WithSource(fromAddress)
.WithDestination(toAddresses)
.WithMessage(message );
try
{
client.SendEmail(request);
}
catch (Amazon.SimpleEmail.AmazonSimpleEmailServiceException sesError)
{
throw new SupplyitException("There was a problem sending your email", sesError);
}
You can refer to below links:
http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp
send email asp.net c#
I hope it will help you. :)

Categories