Mail Format in asp.net? - asp.net

In Web application, I'm trying to send mail to the user with an attachment, I'm sending mail to user but it is not in a good format this is my mail format which is written in asp.net.
StringBuilder stbldr =new StringBuilder();
stbldr.Append("<html><body>
<a href='http://*****/xyxi/mycheck/CheckAttachment.aspx?attid=" + s + "&Eid=" + t + "&afile=" + attach+ "' target='_blank'>Attachment..</a> " + cnt + " <br>
</body> </html>");
when mail send to user, the format in mail is same like above, it is displaying in all tags also like

try
message.Body = stbldr.ToString();
message.BodyFormat = MailFormat.Html;
message.From = txtemail.Text;
message.To = "xxx";
message.Subject = "test";
SmtpMail.SmtpServer = "sxxxxt";
SmtpMail.Send(message);
specify body format as html

Related

Attachment not sent from Server while Locally work's perfectly

Actually i'm trying to send via ASPX using VB.NET as language a mail with attachment that user put in the form via FileUpload which i save to a folder.
All works fine the file is saved and the mail is sent with attachment when i run the site Locally but on the server if i attach something the file is just saved but the email is not sent.
Here is the code where i add the attachment to the mail.
Dim Message As New MailMessage()
Message.To.Add(New MailAddress(reparto))
Message.Subject = oggetto
Message.IsBodyHtml = True
Message.Body =
"<p><h3> " + prodotto + " </h3><br /> " +
"Richiesta di assistenza per il seguente motivo: " + descr +
" <br /> " +
" <br /> " +
" <b>Email cliente: " + email + " </b></p>" +
image.ToString
If FileUpload.HasFiles Then
For Each postedFile As HttpPostedFile In FileUpload.PostedFiles
Dim data As Attachment = New Attachment(Server.MapPath("~/images/") + postedFile.FileName, MediaTypeNames.Application.Octet)
Message.Attachments.Add(data)
'image.Append("<img src=" + """" + (Server.MapPath("~/images/") + postedFile.FileName + """" + "/>"))
Next
End If
Dim client As New SmtpClient()
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try

Create link in email that will redirect to localhost

I'm trying to create a link in my email body which will open a webpage in asp.net.
I know this website is not yet publish because it's still running on a local host,but is it possible to create a link that will redirect to my webpage while my visual studio is running?
here is my code:
private void send_email()
{
MailMessage mm = new MailMessage();
//The email that needs to be checked
mm.To.Add(new MailAddress("******#gmail.com", "Email Check"));
mm.From = new MailAddress("******#gmail.com");
StringBuilder sb = new StringBuilder();
sb.AppendFormat(#"
<h3>We have reviews you transaction</h3>
<br />
<br />
View our reply on Mej website
<br />
Enter your transaction code:
<br />
<h2>" + "bla bla"
+ "</h2> Thank you!<br />"
+ "<small>To view your"
+"transaction please proceed"
+ "<a href='~/Customers/new_web_page/quotation_01.aspx'runat='server'>here</a>"
+"</small>");
mm.Body = sb.ToString();
mm.IsBodyHtml = true;
mm.Subject = "Verification";
SmtpClient smc1 = new SmtpClient();
smc1.Host = "smtp.gmail.com";
smc1.Port = 587;
smc1.UseDefaultCredentials = false;
smc1.Credentials = new NetworkCredential("*********#gmail.com", "*********");
smc1.EnableSsl = true;
smc1.Send(mm);
}
Here is my error whenever I click the link
Add this before StringBuilder sb = new StringBuilder(); statement
var href = System.Web.VirtualPathUtility.ToAbsolute("~/Customers/new_web_page/quotation_01.aspx");
Change
"<a href='~/Customers/new_web_page/quotation_01.aspx'runat='server'>here</a>"
to
"<a href='" + href + "'>here</a>"

In HTML E-Mail, I can't go next line

I have funny problem in showing my emails in my boxes. As i send emails via my host ,it sends to my users successfully but in some part of my email text i want to go to the next line and i used tag to do that. the wieard thing is that in some mailboxes it go to the next line and in some others it doesn't go! for example the correct form is:
Dear Dr smith:
nice....
thank you very much
but it shows like this:
Dear Dr smith: nice.... thank you very much
here is my code:
MailMessage msg = new MailMessage();
StringBuilder bodyMsg = new StringBuilder();
msg.Subject = "Submission Confirmation";
bodyMsg.Append("<html><head><img src=" + "http://javahery21.ir/images/header.jpg" + ">" + "<title>CONFIRMATION EMAIL:</title></head><body>");
bodyMsg.Append("<br/>");
string link = string.Format("http://javahery21.ir/Activate.aspx?userID={0}", userID.ToString());
bodyMsg.Append("Dear " + RadioButtonList_Prefix.SelectedItem.Text + " " + name.Text + " " + middle.Text + " " + lastname.Text + ":<br/> Thank you for registering with Publishing manuscript submission system.");
bodyMsg.Append("To confirm and complete your registration<p>" + " " + "fallow this link" + "</p>.");
//bodyMsg.Append("<br/><br/>");
bodyMsg.Append("<br/>This link is active for 48 hours. If the link is not visited within this time frame, your registration will be discarded and you will need to register again.<br/><br/></br>Best regards,</br>Publishing<br/>http://***.com");
msg.IsBodyHtml = true;
msg.Body = bodyMsg.ToString();
msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
msg.Priority = MailPriority.High;
// msg.ReplyTo = new MailAddress(TextBox2.Text);
msg.From = new MailAddress("***#***.**");
msg.To.Add(new MailAddress(TextBox_email.Text));
SmtpClient mailsender = new SmtpClient();
mailsender.Host = "SmtpClient.***.**";
mailsender.Port = 587;
mailsender.EnableSsl = true;
mailsender.Credentials = new System.Net.NetworkCredential("****#***.**", "classaspnet");
SmtpClient smtp = new SmtpClient();
//Literal1.Text = "<script>alert(' ')</script>";
smtp.Send(msg);
where did i make a mistake?
can any body help me?
Try using <p> tag.
Eg.
<p>Dear Dr smith:</p>
<p>nice....</p>
<br/>
<p>thank you very much</p>
You have missed out this
String msgText = getHtmlMessageText(...);
msg.setContent(msgText, "text/html");
You didnt set the content type for the message

Error while sending mail (attachment file)

in my application i am using to send mail with attachments i write the code like this
Using System.Net.Mail;
MailMessage mail = new MailMessage();
mail.Body = "<html><body><b> Name Of The Job Seeker: " + txtName.Text + "<br><br>" + "The Mail ID:" + txtEmail.Text + "<br><br>" + " The Mobile Number: " + txtmobile.Text + "<br><br>" + "Position For Applied: " + txtPostionAppl.Text + "<br><br>" + "Description " + txtdescript.Text + "<br><br></b></body></html>";
mail.From = new MailAddress ( txtEmail.Text);
mail.To .Add (new MailAddress ( mailid));
mail.Priority = MailPriority.High;
FileUpload1.PostedFile.SaveAs("~/Resume/" + FileUpload1.FileName);
mail.Attachments.Add(filenme);
SmtpMail sm = new SmtpMail();
sm.Send(mail);
it is giving error at attachment like mail.Attachemts.Add(filena)
like this
'System.Collections.ObjectModel.Collection.Add(System.Net.Mail.Attachment)' has some invalid arguments.
Your syntax is wrong. See this article for the correct syntax,
http://www.systemnetmail.com/faq/2.3.aspx
You can't add an attachment simply by specifying the filename. You have to specifically create an attachment object and add that. (as shown in the linked article.)
like this:
mail.Attachments.Add(new Attachment(filename));

ATT00006.dat file autmatically attached in mail attachment

I have a page that have fileupload control, on the submission of the form, when the fileupload control has file, file is sent via attachment in a mail and working absulutly fine, but when the fileupload control does not have file, ATT00006.dat file is automatically sent via email attachment.
Reference URL: http://nextech.pk/Enquiry.aspx?Enq=cu
Advance Thanks for any help
Edit -- Code:
hpf = fup1.PostedFile;
String toEmail = "test#hotmail.com";
String fromEmail = "mailer#hotmail.com";
MailMessage objMail = new MailMessage(fromEmail, toEmail);
objMail.IsBodyHtml = true;
StringBuilder MailBody = new StringBuilder();
MailBody.Append("<html><head></head><body> <br>");
MailBody.Append("<br>" + "An enquiry is filed <br><br>");
MailBody.Append("<strong><u>Enquirer Information</u></strong>" + "<br><br>");
MailBody.Append("<strong>Contact Name:</strong> " + txtFirstName.Text + "<br>");
MailBody.Append("<strong>Email:</strong> " + txtEmail.Text + "<br>");
MailBody.Append("<strong>Institute:</strong> " + txtInstitute.Text + "<br>");
MailBody.Append("<strong>Phone #:</strong> " + txtPhone.Text + "<br>");
MailBody.Append("<br><strong>Description:</strong><br>         " + txtEnquiry.Text + "<br>");
if (hpf != null)
{
MailBody.Append("<br>" + "This email also contains an attachment:- <Strong>(" + hpf.FileName + ")</Strong><br>");
}
MailBody.Append("</body></html>");
objMail.Body = MailBody.ToString();
if (hpf != null)
{
System.IO.Stream inputStream = hpf.InputStream;
String fileName = hpf.FileName;
Attachment attach = new Attachment(inputStream, fileName);
objMail.Attachments.Add(attach);
}
SmtpClient SmtpClnt = new SmtpClient();
SmtpClnt.Send(objMail);
I don't know if you ever got an answer to this, but I've recently studied the problem in detail. The problem occurs because you did not provide an explicit name for the attachment. ASP.NET will always attach as .DAT unless the name is explicitly defined.
The problem is that people assume ASP.NET will use the Filename as the attachment name, which doesn't happen!
In your code, you should create an instance of the attachment, then provide the name explicitly using the FileUpload.FileName property:
Dim att As New System.Net.Mail.Attachment(fu.PostedFile.InputStream, System.Net.Mime.MediaTypeNames.Application.Octet) ' use Octet for binary files '
att.Name = fu.FileName ' get the file name and type automatically '
mm.Attachments.Add(att)
A full explanation of ASP.NET attaching .DAT files is available here
Its a mis-match in the attachment type that the system doesn't understand. Please post your code and what you do when there is not file as an attachment.
I think the mail server you are using (or antivirus software used by the mail server) is automatically adding this file.
Does the file in question contain anything, or is it empty?

Resources