failed sending template within email - asp.net

in asp.net, i am trying to send a template in an email, so when a user will get a message from my website, then it will be in a template,
e.g. when FB sends you a notification via mail, then it has a header and footer too, so i want to do that, i tried but it isn't sending any template within email body
CODE:
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "emailBody.htm";
mailDefinition.From = "hunain.hafeez#gmail.com";
MailMessage message = new MailMessage();
message.From = new MailAddress(user_email);
message.To.Add(new MailAddress(email));
message.Subject = "Registration";
message.IsBodyHtml = true;
message.Body = "Congrats" + " " + userName + " " + " You have been shortlisted for interview. Please appear on " + " " + date +", "+time+" "+ "for interview";
SmtpClient objClient = new SmtpClient("smtp.gmail.com", 587);
objClient.EnableSsl = true;
objClient.Credentials = new System.Net.NetworkCredential("hunain.hafeez", "*******");

Short Answer: For quick fix use HTML mark-up templates , however a more flexible solution is Razor with email templating.
There are different approaches on how to achieve this. However, I stumbled upon a greate article about how to use Razor with email templating. Razor was pushed with ASP.NET MVC 3, but MVC is not required to use Razor. This is pretty slick processing of doing email templates
As the article identifies, "The best thing of Razor is that unlike its predecessor(webforms) it is not tied with the web environment, we can easily host it outside the web and use it as template engine for various purpose. "
Use Razor for Email Template outside ASP.NET MVC

Your message body needs to have HTML markup in it.
ie:
<html><body><b>Congrats ....

Related

Sending email with embedded images using Sendgrid on Azure

I have an ASP.NET website published to Azure from which emails can be sent. Some are plain text but I added a welcome email that is newsletter style with embedded images. The code is VB.NET. I have the system working nicely on our development server displaying a preview before sending the email. On Azure, the code to send the newsletter email is not working. The plain text email goes OK and I have tested the newsletter as an HTML email without the embedded images and that goes through OK. The preview can find the images so I am sure they are there and can be accessed. I do not get any error message, the email just never shows up in the sendgrid account as being processed. The code is as follows:
Try
Dim mymessage = New SendGridMessage
mymessage.From = New MailAddress("do-not-reply#company.co.uk")
mymessage.AddTo(txtemail.Text)
mymessage.Subject = "Welcome Email"
mymessage.Text = plaintext
mymessage.Html = htmlBody
Dim arrct As Integer = arrImages.Count - 1
For i As Integer = 0 To arrct
mymessage.AddAttachment(arrImages(i).ipath)
mymessage.EmbedImage(arrImages(i).fname, arrImages(i).id)
Next
Dim username = ConfigurationManager.AppSettings("emailServiceUserName")
Dim pswd = ConfigurationManager.AppSettings("emailServicePassword")
Dim credentials = New NetworkCredential(username, pswd)
Dim transportweb = New Web(credentials)
transportweb.DeliverAsync(mymessage)
'code here to display success message
Catch exc As Exception
'error code here
End Try
The array of images is populated with a number of images located in a folder as they don't change, like so:
Dim research As String = Server.MapPath("~\ImageTemp\" + query.ImageName)
'Extend the array
ReDim Preserve arrImages(i + 1)
arrImages(i + 1).ipath = respath
arrImages(i + 1).fname = qry.ImageName
arrImages(i + 1).id = "img" & i + 1
I have checked the web and can find others who have problems where the code works on the local server but not on Azure but no answers that help with this specific questions. It must be to do with the way I am handling the images but I can't see it.
Have reviewed
Unable to send emails on Azure Web App using Sendgrid
How to send embedded images with sendgrid emails?
Sending an email with attachment using SendGrid
The answer is to change the addattachment code to this :
Dim arrct As Integer = arrImages.Count - 1
For i As Integer = 0 To arrct
mymessage.AddAttachment(Server.Mappath("~\ImageTemp\" & arrImages(i).fname)
mymessage.EmbedImage(arrImages(i).fname, arrImages(i).id)
Next

opening small size window from link send in body of email generated using asp.net

here is my code
System.Text.StringBuilder sb = new System.Text.StringBuilder();
MailMessage message = new MailMessage("abc#gmail.com", txtEmailId.Text.Trim());
message.Subject = "Auto email Test";
message.IsBodyHtml = true;
string str="http://localhost:55243/WebSiteTest/Accept.aspx?id=" + txtEmailId.Text.Trim();
string url = #"<a href="""+str+#""" target='_blank'";
string str1 = "http://localhost:55243/WebSiteTest/Reject.aspx?id=" + txtEmailId.Text.Trim();
string url1 = #"<a href=""" + str1 + #""" target='_blank'";
message.Body = #"<html><body>Thanks For Showing interest in our site. please press "+ url + #">Accept</a>Or "+ url1+">Reject</a></body></html>";
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("abc#gmail.com", "password");
client.Send(message);
it redirects to the aspx page when link is clicked from mail received but as a new tab in browser.
but i need to open the pages in small window that will open on the screen.
any help!!!!!
Thanks in Advance.
You can't do that. You would need javascript to open a window at a certain size, and e-mail cients - reasonably so - do not run javascript in e-mails.
I believe what you're seeing is a combination of markup and browser behavior. In particular, the anchor tags you're generating in the message body both contain the target="_blank" attribute. This tells the browser to open the content in a new window, but doesn't specify the size of the window, so it seems the browser is just opening a new tab in an already opened browser (if no browser is opened a new browser instance altogether is opened).
The behavior you're seeing is not unexpected. As Menno van den Heuvel mentioned, you do need JavaScript to give browsers explicit instructions on the window size. This w3schools.com link has some details on that if you're interested. But, again, as Menno van den Heuvel points out most email clients aren't well equipped to handle JavaScript.

receive email through asp.net application

I want to receive email to my email address through my asp.net application. Its like someone who send an inquiry through a form. I have used the following code for this and seems like its doing nothing. I remember that I did one of my websites and cant remember how I did it. Please find the code below.
Thanks,
eMessage.To = "info#boilernetworkservices.co.uk"
eMessage.From = txtEmail.Text
eMessage.Subject = "Web Submission"
eMessage.Body = "Web submission received from " & txtName.Text & ". Phone no: " & txtPhone.Text & "."
eMessage.Priority = MailPriority.High
SmtpMail.Send(eMessage)
How can I make this working?
Your sample code shows how to use SMTP to send emails, but you won't be able to retrieve emails from a remote server using this protocol.
The two most common protocols used to retrieve emails are POP3 and IMAP4, and unfortunately the .NET framework doesn't provide an implementation of them like it is done with SMTP.
One option for email retrieval is to use use the open source POP3 client OpenPop.NET, which is discussed in this SO question: retrieve email using c#?.
I set up a google account for my business like myCompanyName#gmail.com.
And I use that as a relay.
You have to set your google account to "Allow less secure apps".
Here is my code to let a potential client fill out a contact us from and send the info to me (Even works when I publish to Azure:)):
private void SendEmailToMyCompany(ContactInfo contactInfo)
{
string message = contactInfo.Message.Replace("\n", "<br />");
MailAddress from = new MailAddress(contactInfo.Email);
MailAddress to = new MailAddress("myhotmailaccount#hotmail.com");
MailMessage mailMessage = new MailMessage(from, to);
StringBuilder body = new StringBuilder();
body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
body.Append("<br />");
body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
body.Append("<br />");
body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
body.Append("<br />");
body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
body.Append("<br />");
body.AppendFormat($"<b>Message:</b><br /><br /> {message}");
mailMessage.Body = body.ToString();
mailMessage.Subject = "MyCompany Customer Contact";
mailMessage.IsBodyHtml = true;
string smtpHost = _config["EmailSettings:SmtpHost"];
string port = _config["EmailSettings:Port"];
string userName = _config["EmailSettings:UserName"];
string password = _config["EmailSettings:Password"];
SmtpClient client = new SmtpClient(smtpHost)
{
Port = int.Parse(port),
Credentials = new NetworkCredential(userName, password),
EnableSsl = true
};
client.Send(mailMessage);
}
And then here is my email settings from app.config:
"EmailSettings": {
"SmtpHost": "smtp.gmail.com",
"Port": 587,
"UserName": "myCompanyNameGmailAccount#gmail.com",
"Password": "**********"
}

aspx email photo submission

I am new to asp.net, I am trying to have a photo contest on my website.
I want users to upload their pictures on my site and I want to get an email with the picture attachment to my email ID.
How can I do this and is there a better way of doing this(I have to use asp for this)
Thanks,
jack
It should be relatively simple by using the FileUpload webcontrol on the aspx form and using System.Net.Mail.SmtpClient to send a MailMessage with an attachment.
Well if you wanna do it in vb you would do something like this :
Dim message As New Mail.MailMessage
Using message
message.To.Add(New Mail.MailAddress("notyou#gmail.com", "Name Reciever"))
message.From = New Mail.MailAddress("you#gmail.com", "Name Sender")
message.Subject = "subject!"
message.Body = "Text!"
message.Attachments.Add(New Mail.Attachment("pic.jpg"))
Using smtp As New Net.Mail.SmtpClient("Your smtp")
smtp.Send(message)
End Using
But you prob use c# but you din't say so ... but it prob looks alike anyhow ^^

E-Mails sent from online form are coming in one single mail

I'm using an online form at one of my web sites. Every mail sent from this form is coming in one mail even if the senders IP is different.
But I want every single mail to be unique even if the content is same. What I need to do to the mails or which header I need to modify?
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("no-reply#toprakbasim.com", "NoReply");
MailAddress toAddress = new MailAddress("info#toprakbasim.com", "Info");
MailAddress toSender = new MailAddress(tEMail.Text, tNameSurname.Text);
message.From = fromAddress;
message.Bcc.Add(toAddress);
message.ReplyTo = toSender;
message.Subject = tNameSurname.Text + " : contact";
message.IsBodyHtml = true;
message.Body = "some html here";
smtpClient.Send(message);
Gmail will group emails with the same subject line together. Put some text in the subject line to make it unique, like MessageID, time, whatever.
If you are saying the content in the body contains more than one response, then the issue is how you are collecting the text that then gets assigned to message.Body. If the text is in a variable before assigning to message.Body, make sure that you are not re-using the variable and that it gets re-instantiated each time.
What are you using for a mail reader program? Because it sounds like that program is rolling your emails up for you. (Outlook 2010 does this, by default).
Try reading your email with a different email reader (like Outlook Express, or tbird)

Resources