email working local but not on server - c#

I'm trying to send out an email after a button click event. When testing from my local machine everything runs smooth, the email get send out and I get the popup message notifying me that the "call has been resolved". But running from server side the page "hangs" - the screen is dimmed out which should happen until the email is sent off, but it stays like that. the email does not get sent out from server side....
This is what I've done:
My code:
MailMessage Mail = new MailMessage();
Mail.Subject = "Call Resolved";
Mail.To.Add(useremail);
// Mail.To.Add(useremail);
Mail.From = new MailAddress("notifications#oep.co.za");
// string path = Server.MapPath(#"..\Images\SOSLetterhead.png");
// string path = PopulateEmailImage();
string path = Server.MapPath(#"\Images\ItsmBCXHeader.gif");
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "header";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><head>" +
#"<meta content=""text/html; charset=utf-8"" http-equiv=""Content-Type""/> " +
#"<title></title></head><body ><style> " +
#".auto-style1 {width: 188px; }table tr td{ border:solid;border-width:1px;} .link{color:red;}</style> " +
#"<img alt="""" height=""130"" src=""cid:header"" width=""675"" /> " +
#"<div style=""color:red; font-weight:bold; text-align:left; border:none; margin-right:20%;"" > " +
#"<h3>This message is sent on behalf of <br /> The Business Connexion Global Service Management Centre</h3> " +
#"<h5><i>Please do not respond to the sender as this mailbox does not receive return mail. <br /> " +
#"Use the Link located at the bottom of the e-mail to Respond </i></h5> </div><br />" +
#"<div>Dear <b>" + CustName + "</b></div><br /> " +
#"<div>We are pleased to inform you that your reported Incident <b>" + incidentNo + "</b>, has been resolved. </div><br /> " +
#"<div><b>Incident Summary report as follows:</b></div> <br /> " +
#"<table style=""width:45%; border:solid; border-width:3px; background-color:#E2E2E2;""> " +
#"<tr><td class=""auto-style1""><b>Incident Number:</b></td><td>" + incidentNo + "</td> " +
#"<tr><td class=""auto-style1""><b>Status:</b></td><td>" + stats + "</td></tr> " +
#"<tr><td class=""auto-style1""><b>CI Serial No:</b></td><td>" + serialNo + "</td></tr> " +
#"<tr><td class=""auto-style1""><b>Incident Summary:</b></td><td>" + incidentSum + "</td></tr> " +
#"<tr><td class=""auto-style1""><b>Incident Notes:</b></td><td>" + incidentNotes + "</td></tr> " +
#"<tr><td class=""auto-style1""><b>Resolution:</b></td><td>" + resolution + "</td></tr> " +
#"</table><br /><div> " +
#"If you have any queries or if you would like to change your contact details, please contact the <br /> Global Service Management Centre. " +
#"</div><br /><div> " +
#"Click here if you would like to contact the Global Service Management Centre via e-mail </div> " +
#"<br /><div><b>011 266 1102</b> (National, South African number) <br /><b>+27 (0) 266 1102</b> (International number)<br /> " +
#"E-mail queries to spoc#bcx.co.za <br /></div><br /><div><b>Yours in service <br /> " +
#"Global Service Management Centre</b></div></body></html>", null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
Mail.AlternateViews.Add(av1);
System.Configuration.Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
int port = mailSettings.Smtp.Network.Port;
string host = mailSettings.Smtp.Network.Host;
string password = mailSettings.Smtp.Network.Password;
string username = mailSettings.Smtp.Network.UserName;
}
SmtpClient SMTP = new SmtpClient();
SMTP.Send(Mail);
And my web.config:
<mailSettings>
<smtp deliveryMethod="Network" from="Notifications#smart-office.co.za">
<network host="host" userName="notifications#oep.co.za" password="password" port="25" />
</smtp>
</mailSettings>
I have removed the password and host for security reasons... Any help will be greatly appreciated! this works on another site, but not on this one.....

I had once this problem, I changed the value of EnableSsl field of SmtpClient to false before publishing and it worked for me, If you already have false then change it to true

Check in windows Event viewer for more details. it might log information if its problem with environment.

Related

Using apostrophe in email subject while sent email via gmail api creating an issue

While sent email using below subject which apostrophe replacing with another characters
Actual Subject : We’ll make 100,800 cold calls for you
Mail Shows Subject : We’ll make 100,800 cold calls for you
Issue happens when I'm sent email via api , when sent email from SMTP it's working fine
Please check my api code below
string msg = "From: " + FromName + "<" + From + ">" + " \r\n" +
"To: " + ToName + "<" + To + ">" + " \r\n" +
"BCC: " + BCCEmail + " \r\n" +
"Subject: " + Subject + " \r\n" +
"Message-ID: mID_" + messageID + "\r\n" +
"References: "+encryptMessageID + "\r\n" +
"In-Reply-To: " + encryptMessageID + "\r\n" +
"Content-Type: " + contentType + "; charset=us-ascii\r\n\r\n" + Body;
dynamic objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg) };
if (!string.IsNullOrEmpty(messageThreadID))
objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg), threadId = messageThreadID };
var _objSendMsg = JsonConvert.SerializeObject(objSendMsg);
var strSendMsg = new StringContent(_objSendMsg, UnicodeEncoding.UTF8, "application/json");
When same content i'm applying in body with apostrophe working fine for body
Please check attached screenshot
Email copy
You need to base64_encode of the subject header your sending it as plain text. the API is getting confused.
Subject: " + Convert.ToBase64String(Subject) + " \r\n" +

How to format Asp.net SMTP Email with HTML?

I was doing an SMTP email that will send the order details to the customer email after the purchase. I had successfully sent it to the customer but the format is a little weird.
Below is the output it sends to the customer email.
Your order is successful!
------------------------------------------------
Order ID : Order20465230820207quiw< br />Order Date : 8/30/2020< br />Send To : 81,JALAN KENARI MERAH 8
<br /> Kedah< br /> 05200< br />Grand Total : 499< br />< br />< br />Thank You for purchasing with us!
But it expects it to format like this :
Your order is successful!
------------------------------------------------
Order ID : Order20465230820207quiw
Order Date : 8/30/2020
Send To : 81,JALAN KENARI MERAH 8
Grand Total : 499
Thank You for purchasing with us!
Here is the code:
lblmail.Text = "Your order is succesful! <br />" +
"------------------------------------------------ <br />" +
"Order ID : " + Session["OrderID"].ToString() + "< br />" +
"Order Date : " + Session["orderDate"].ToString() + "< br />" +
"Send To : " + Session["address"].ToString() + "< br />" +
" " + Session["state"].ToString() + "< br />" +
" " + Session["zipcode"].ToString() + "< br />" +
"Grand Total : " + Session["GrandTotal"].ToString() + "< br />" +
"< br />< br />Thank You for purchasing with us!" ;
Do note that I had enabled the HTML is the email body with this code:
MailMessage mm = new MailMessage();
mm.IsBodyHtml = true;
What is the problem here? The code seems to be fine but the output is not what I expected.
You need to add HTML insted of normal text
Code
var htmlContent ="<!DOCTYPE html>
<html>
<body>
<h3><p>Your order is successful!</p></h3>
<p>----------------------------------</p>
<p>Order ID : Order20465230820207quiw</p>
<p>Order Date : 8/30/2020</p>
<p>Send To : 81,JALAN KENARI MERAH 8</p>
<p>Grand Total : 499</p>
<p>Thank You for purchasing with us!</p>
</body>
</html>";
using (MailMessage mm = new MailMessage())
{
mm.Body = htmlContent;
mm.IsBodyHtml = true;
}
It's working fine with your mail functionality
make sure that your HTML tags are written correctly "< br />" remove the spaces, with is this way you make sure that the server dose not send it as text.
< br /> To this <br/>

Send a mail from web server using asp.net? Not working in Godaddy webserver

I have a website, there is a Contact Us tab. User have to fill the form and when click the button I should get a mail from user.
It's working fine when I am running from localhost. But the same code it is not running in Godaddy webserver. This is my code
try
{
MailMessage mailMessage = new MailMessage("From#gmail.com", "To#mydomain.com");
mailMessage.Subject = txtSubject.Text;
string msg = "#Sender Name: " + txtUserName.Text + "#Sender Email ID: " + txtUserEmailId.Text + "#Company Name: " + txtCompanyName.Text + "#Mobile No: " + txtMobileNo.Text + "#Office Telephone No: " + txtOfficeTelephoneNo.Text + "#Office Fax No: " + txtOfficeFaxNo.Text + "#City: " + txtCity.Text + "#Sender Message: " + txtMessage.Text;
string newMsg = msg.Replace("#", "\n");
mailMessage.Body = newMsg;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "From#gmail.com",
Password = "Password"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
}
catch (Exception ee)
{
Response.Write(ee.Message);
}

Pass a TextBox Value as a link and a parameter to another webform

I am trying to send a email from my webform1 once a new work order has been created, however when the user clicks the workorder number i want that workorder number to be passed as a parameter to webform2. the code i use to send the email:
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.From = new System.Net.Mail.MailAddress("some#some.com");//who send
mm.To.Add(new System.Net.Mail.MailAddress("some#gmail.com"));
mm.To.Add(new System.Net.Mail.MailAddress("some#some.com"));
mm.Subject = "WorkOrders Created With Type Safety";
mm.Body = "WorkOrderNumber" + ": "
+ "<a href=\"http://localhost:49695/Safety.aspx?=WorkOrderNum=\">"
+ TextBox13.Text + "</a>"
+ "<-Click on the Work Order Number" + "<br><br/>"
+ "Requestor" + ": " + TextBoxRequestor.Text
+ "<br><br/>" + "Date" + ": " + TextBoxDate.Text
+ "<br><br/>" + "Department" + ": " + TextBoxDepartment.Text
+ "<br><br/>" + "Completion Date" + ": "
+ TextBoxCompletionDate.Text + "<br><br/>" + "Machine Description"
+ ": " + TextBoxMachineDescription.Text + "<br><br/>"
+ "Machine Location" + ": " + TextBoxMachineLocation.Text
+ "<br><br/>" + "Work Required" + ": " + TextBoxWorkRequired.Text
+ "<br><br/>" + "Status" + ": " + TextBoxStatus.Text;
mm.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = ConfigurationManager.AppSettings["smtpServer"];
client.Send(mm);
when the above code is sent as an email the result looks like this and the 14 is clickable link:
WorkOrderNumber: 14<-Click on the Work Order Number
Requestor: Mark
Date: 01/07/2015
Department: IT
Completion Date: 01/08/2015
Machine Description: Fan
Machine Location: SomeLocation
Work Required: Needs Installing
Status: Open
so then on webform2 i would be able to use this:
protected void Page_Load(object sender, EventArgs e)
{
var workourdernum = Request.QueryString["WorkOrderNum"];
}
so i just want that number from TextBox13.Text be passed along with the link, i looked online people are accomplishing similar task via Response.Redirect()
Will this work?
"<a href=\"http://localhost:49695/Safety.aspx?WorkOrderNum=" +
+ TextBox13.Text + "\">" + TextBox13.Text + "</a>"
+ "<-Click on the Work Order Number" + "<br><br/>"
You'd be passing the work order number in the query string. Your web page could then just extract the work order number from the query string.
You'll get HTML that looks like this:
123
The code you already have (for Page_Load) would work perfectly to extract the work order number in the second page.
Here is example how to pass value from one form to another.
webform1.aspx - for example on button click event
Response.Redirect("webform2.aspx?number=" + TextBox13.Text);
webform2.aspx
int passedNumber;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["number"] != null)
{
passedNumber = Convert.ToInt32(Request["number"]);
}
}
Now you can do what ever you want with that number on webform2.

I'm having problems with SMTP Authentication in C#

I have an MVC 3 app and everything I try on my new hosting provider ends up throwing this exception:
Server Error in '/' Application.
Mailbox unavailable. The server response was: Authentication is required for relay
I tried using the code from
How can I make SMTP authenticated in C#
which has many up votes, but I still get the exception.
My host has the typical panels that let me create mail accounts. I'm not sure about creating NetworkCredentials, what do I use as the User Name and password? What I've been using is the email address and the password for the email account.
Here's my code:
public static void sendMail(Joiner request) {
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("garbage#stopthedumpcoalition.org", "REDACTED");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("garbage#stopthedumpcoalition.org");
smtpClient.Host = "mail.stopthedumpcoalition.org";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "Join the Coalition request from - " + request.FirstName + " " + request.LastName;
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "Name: " + request.FirstName + " " + request.LastName + "<br>"
+ "EMail: " + request.Email + "<br>"
+ "Wants to Volunteer: " + request.Volunteer.ToString() + "<br>"
+ "Organization: " + request.Organization + "<br>"
+ "Wants to become a Partner: " + request.Partner.ToString() + "<br>"
+ "Comments: " + request.Comments;
message.To.Add("nivram509#gmail.com");
smtpClient.Send(message);
}
Thanks Sani. It was even stupider than that. My Site Host's web panel had two text boxes for changing the password, I I filled both out and hit save.
It also had a checkbox for "change Password" and I never noticed it.
So I had the wrong password. The code is fine. :)

Categories