How to send an email from an ASP.NET MVC view page with an attachment? - asp.net-mvc-2

I have to send an email from my ASP.NET MVC 2 contact form view page.I need a detail answer that describes how to create the model , the controller and the view for that purpose .. Here is the code i have given in my controller class's action method..
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SendEMail(CareersEMailModel careersEMailModel,HttpPostedFileBase upload)
{
if (ModelState.IsValid)
{
bool isOK = false;
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("no-reply#abc.com", "Website contact form");
msg.To.Add("info#abc.com");
msg.Subject = "Resume";
string body = "Name:" + careersEMailModel.Name + "\n" + "Phone:" + careersEMailModel.Phone + "\n" + "Email:" + careersEMailModel.Email;
string file = careersEMailModel.Resume;
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("mailserver_url.net", 25);
smtp.Send(msg);
msg.Dispose();
isOK = true;
CareersMessageModel rcpt = new CareersMessageModel();
rcpt.Title = "Email sent successfully!!";
rcpt.Content = "Your details has been received with great thanks.We'll contact you as soon as possible.";
return View("CareersMessage", rcpt);
}
catch (Exception ex)
{
CareersMessageModel err = new CareersMessageModel();
err.Title = "Sorry,Email sending failed!!!";
err.Content = "The website is having an error with sending this mail at this time.You can send an email to our address provided in our contact us form.Thank you.";
return View("CareersMessage", err);
}
}
else
{
return View();
}
}

For retrieving the uploaded file, you will need to do this
foreach (string file in Request.Files)
{
var uploadFile = Request.Files[file];
if (uploadFile.ContentLength == 0) continue;
string fileLocation = //File Location with file name, needs to be stored for temporary purpose
uploadFile.SaveAs(fileLocation);
}
Then with help of following code you can attach file
Attachment data = new Attachment(fileLocation, MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
Once done with the email delete the file created on server.
Hope this answers your question

from MSDN
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = "data.xls";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane#contoso.com",
"ben#contoso.com",
"Quarterly data report.",
"See the attached spreadsheet.");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString() );
}
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
}
EDIT:
the Attachment class accepts a stream. So try this. (i haven't tested it but it should give you the gist of what you need to do)
foreach (string fileName in Request.Files)
{
HttpPostedFile file = Request.Files[fileName];
Attachment data = new Attachment(file.InputStream, fileName);
// do stuff to attach it to the Mail Message
}

Related

How to send multiple email with attachments using send grid

I'm using the send grid api to send emails in my c# app, and I can't seem to figure out how to send an email with multiple attachments. I can send an email with a single attachement, but not multiple. I can see that there are four methods used for attachments(see below), but there is only one that allow you to send a list of attachements "IEnumerable attachments", however it's a return type is void. Can someone tell me how to send multiple attachments using the send gris api?
[![Send Grid methods for addding attachments](https://i.stack.imgur.com/Obf3n.png)](https://i.stack.imgur.com/Obf3n.png)
public async Task<SendResponse> SendHTMLWithAttachmentSendgrid(List<EmailAddress> recipientEmailList, string subject, string htmlContent, string[] attachmentFilePaths, bool showAllRecipients = false)
{
SendResponse emailResponse = new SendResponse();
try
{
List<SendGrid.Helpers.Mail.Attachment> attachments = new List<SendGrid.Helpers.Mail.Attachment>();
SendGrid.Helpers.Mail.Attachment attach = null;
//The total size of your email, including attachments, must be less than 30MB.
foreach (string item in attachmentFilePaths)
{
var stream = File.OpenRead(item);
//string[] contentArray = fileName.Split('.');
//string contentType = contentArray[1].ToString();
var streamCount = stream.ToString();
System.IO.BinaryReader br = new System.IO.BinaryReader(stream);
byte[] bytes = br.ReadBytes((Int32)stream.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
string[] filePathArray = item.Split('\\');
string fileName = filePathArray.LastOrDefault();
attach = new SendGrid.Helpers.Mail.Attachment() { Filename = fileName, Content= base64String };
attachments.Add(attach);
}
var from = new EmailAddress(DefaultSenderEmailAddress);
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, recipientEmailList, subject, string.Empty, htmlContent, showAllRecipients = false);
MailHelper.CreateSingleEmailToMultipleRecipients(from, recipientEmailList, subject, string.Empty, htmlContent, showAllRecipients = false).AddAttachments(attachments);
var response = await this.SendGridClient.SendEmailAsync(msg);
}
catch (Exception ex)
{
_logger.LogError($"The following error occurred:{ex.Message}.");
}
return emailResponse;
}
I'm expecting to send multiple attachments in my email, using the Send Grid Api

get files from IFormFile for email attachment

I have an upload controller
// Multiple files upload
[HttpPost("upload/multiple")]
public IActionResult Multiple(IFormFile[] files)
{
try
{
// Put your code here
return StatusCode(200);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
I am trying to use the IFormFile to get files for email attachments using MailKit
This is my email code
private async Task SendEmail()
{
try
{
// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(sender));
email.To.Add(MailboxAddress.Parse(receiver));
email.Subject = emailsubject;
var multipart = new Multipart("mixed");
multipart.Add(new TextPart(TextFormat.Html) { Text = emailMessage });
foreach (var attachment in files)
{
var content = new MemoryStream();
attachment.CopyTo(content);
content.Position = 0;
var contentType = ContentType.Parse(attachment.ContentType);
var part = new MimePart(contentType.MimeType)
{
FileName = Path.GetFileName(attachment.FileName),
ContentTransferEncoding = ContentEncoding.Base64,
Content = new MimeContent(content),
};
multipart.Add(part);
}
email.Body = multipart;
//email.Body = new TextPart(TextFormat.Html) { Text = emailMessage};
// send email
using var smtp = new SmtpClient();
smtp.Connect(outgoingServer, outgoingPort, SecureSocketOptions.Auto);
smtp.Authenticate(userName, userPassword);
smtp.Send(email);
smtp.Disconnect(true);
}
catch (Exception ex)
{
NotificationService.Notify(NotificationSeverity.Error, "Send Email Error!", ex.Message, 7000);
}
}
}
This generates an error on the 'files' variable
"The name 'files' does not exist in the current content.
Can someone tell me what I am missing to pull the information from the controller for 'files'???
Here, in the Async Task SendEmail task, we have to set the parameter of type IFormFile like below,
private async Task sendEmail(IFormFile file)
Here is the Controller Code,
[HttpPost("send")]
public async Task<IActionResult> SendMail(IFormFile file)
{
try
{
await mailService.sendEmailAsync(file);
return Ok("Mail Sent!");
}
catch (Exception ex)
{
throw;
}
}
And We can't get IFormFile[] directly and pass it to the sendEmail method as a parameter. Instead we can create the model class with public List<IFormFile>? attachments{get;set;} and send the instance of the model class as a parameter to the sendEmail method.

How to get IFormFile data into stream to add as Email Attachment

I have an Email Razor page that accesses a custom method to send and email. All code seems to be correct. The problem I am having is accessing the "IFormFile files". I have tried adding the code to a controller as well as adding the IFormFile files to the custom method. I just can seem to find the right way to hande IFormFile. Below is the code that I currently have. What I am looking for is to see if someone can help me with the IFormFile portion. The information on IFormFile that I have found hasn't helped me so far as it's usually only partial information...I am very new to MVC and IFormFile, so help is greatly appreciated!
private async Task SendEmail()
{
try
{
// create email message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(sender));
email.To.Add(MailboxAddress.Parse(receiver));
email.Subject = emailsubject;
var multipart = new Multipart("mixed");
multipart.Add(new TextPart(TextFormat.Html) { Text = emailMessage });
foreach (var attachment in file)
{
var content = new MemoryStream();
attachment.CopyTo(content);
content.Position = 0;
var contentType = ContentType.Parse(attachment.ContentType);
var part = new MimePart(contentType.MimeType)
{
FileName = Path.GetFileName(attachment.FileName),
ContentTransferEncoding = ContentEncoding.Base64,
Content = new MimeContent(content),
};
multipart.Add(part);
}
email.Body = multipart;
//email.Body = new TextPart(TextFormat.Html) { Text = emailMessage};
// send email
using var smtp = new SmtpClient();
smtp.Connect(outgoingServer, outgoingPort, SecureSocketOptions.Auto);
smtp.Authenticate(userName, userPassword);
smtp.Send(email);
smtp.Disconnect(true);
}
catch (Exception ex)
{
NotificationService.Notify(NotificationSeverity.Error, "Send Email Error!", ex.Message, 7000);
}
}

Mandrill.Net Adding Multiple Attachments

I am trying to send an email via Mandrill.Net, and am getting stuck trying to add multiple attachments. I have got the following code that create the list of attachments as IEnumerable, but I am getting the error
Unable to cast object of type 'System.Collections.Generic.List`1[Mandrill.Models.Attachment]' to type 'System.Collections.Generic.IEnumerable`1[Mandrill.Models.EmailAttachment]'.
The code is below:
try
{
EmailService ems = new EmailService();
EmailMessage msg = new EmailMessage();
List<EmailAddress> ToAdd = new List<EmailAddress>();
EmailAddress MainTo = new EmailAddress();
MainTo.Email = qe.ToAddress;
MainTo.Type = "bcc";
ToAdd.Add(MainTo);
msg.FromEmail = qe.FromAddress;
msg.FromName = qe.FromName;
msg.AddHeader("Reply-To", qe.ReplyTo);
msg.To = ToAdd;
msg.Subject = qe.Subject;
msg.Html = qe.Body;
msg.TrackClicks = true;
msg.TrackOpens = true;
// Need to add in Email Attachments
List<EmailAttachment> lea = new QueuedEmailModels().GetEmailAttachmentsByQueuedEmailID(qe.QueuedEmailsID); // Gets List of Attachments in DB
List<Attachment> lma = new List<Attachment>();
foreach(BRPA.EmailAttachment ea in lea)
{
byte[] array = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/UploadedFiles/" + ea.AttachmentID));
Attachment at = new Attachment
{
Content = Convert.ToBase64String(array),
Name = ea.AttachmentName,
Type = ""
};
lma.Add(at);
}
msg.Attachments = (IEnumerable<Mandrill.Models.EmailAttachment>)lma.AsEnumerable();
// End of Attachments
await ems.SendMailviaMandrill(msg);
qe.Status = "Sent";
qe.DateTimeSent = DateTime.Now.AddHours(2);
await ctx.SaveChangesAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Thanks
Paul
Oops - Found the answer.
I had a class for EmailAttachment that was overriding the Mandrill.Models.EmailAttachment.
Specified the full name and all is good.

Javamail Read Body of Message Until EOF

The following code is supposed to send and save messages sent via yahoomail. The sending part works OK but id does not save the sent message. I've been researching and found the following code:
/**
* Read the body of the message until EOF.
*/
public static String collect(BufferedReader in) throws IOException {
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
}
How do I incorporate it in the following code?
public void doSendYahooMail(){
from = txtFrom.getText();
password= new String(txtPassword.getPassword());
to = txtTo.getText();
cc = txtCC.getText();
bcc = txtBCC.getText();
subject = txtSubject.getText();
message_body = jtaMessage.getText();
//String imapHost = "imap.mail.yahoo.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.imap.host", "imap.mail.yahoo.com");
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.port", "993");
Session session = Session.getInstance(props,null);
try {
// message definition
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
if(!cc.equals("")){
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
if(!bcc.equals("")){
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
message.setSubject(subject);
if(!filename.equals("")){// if a file has been attached...
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message_body);// actual message
Multipart multipart = new MimeMultipart();// create multipart message
// set the text message part
multipart.addBodyPart(messageBodyPart);//add the text message to the multipart
// attachment part
messageBodyPart = new MimeBodyPart();
String attachment = fileAbsolutePath;
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);//add the attachment to the multipart message
// combine text and attachment
message.setContent(multipart);
// send the complete message
Transport.send(message, from, password);
}
else{// if no file has been attached
message.setText(message_body);
Transport.send(message, from, password);
}
JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);
filename = "";//reset filename to null after message is sent
fileAbsolutePath = "";//reset absolute path name to null after message is sent
// save sent message
Store store = session.getStore("imap");
store.connect(from, password);
Folder folder = store.getFolder("Sent");
if(!folder.exists()){
folder.create(Folder.HOLDS_MESSAGES);
Message[] msg = new Message[1];
msg[0] = message;
folder.appendMessages(msg);
}
store.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
}