in 3.5 .Net Framework i have a problem to sending an email on server.
Code-
MailMessage message = new MailMessage();
message.From=new MailAddress("CMC Enquiry" + "<my1#domain.in>");
message.To.Add(new MailAddress("vishalpatel8086#gmail.com"));
message.CC.Add(new MailAddress("varun_aone#yahoo.com"));
message.Subject = "Enquiry from CMC site";
string p = "<b>Name: </b>" + TextBox1.Text;
p += "<br><b>Mobile:</b> " + TextBox3.Text;
p += "<br><b>Mail ID:</b> " + TextBox2.Text;
p += "<br><b>Address:</b> " + TextBox4.Text;
p += "<br><b>City:</b> " + TextBox5.Text;
p += "<br><b>Location:</b>" + locationlst.SelectedItem.Text;
p += "<br><b>College:</b> " + TextBox11.Text;
p += "<br><b>Course:</b> " + DropDownList3.SelectedItem.Text;
p += "<br><b>Query:</b> " + TextBox12.Text;
message.Body = p;
message.IsBodyHtml = true;
SmtpClient SMTPServer = new SmtpClient("localhost");
// try
// {
SMTPServer.Send(message);
//result = "Your Enquiry has been Submitted !!";
Label5.Text = "Your Enquiry has been Submitted !!";
//Response.Write("<script language=JavaScript> alert('Your Enquiry has been Submitted !!'); </script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox11.Text = "";
TextBox12.Text = "";
DropDownList3.SelectedIndex = 0;
// }
// catch
// {
// Label5.Text = "Low Server Problem !!Your Enquiry Not Submitted";
//Response.Write("<script language=JavaScript> alert('Server Problem !!Your Enquiry Not Submitted'); </script>");
// }
}
else
{
Label5.Text = "Server Problem !!Your Enquiry Not Submitted";
}
}
This code is working my other web hosting server with my different website but is not working with new web site . A error is being occured like -
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Error Is -
Line 83: SMTPServer.Send(message);
Source File: c:\inetpub\vhosts\cmcent.in\httpdocs\enquiry.aspx.cs
Line: 83
Your SmtpClient code needs to be revised as follow:
//Set your smtp client settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.yourdomain.com"; //set with your smtp server
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("fromEmail#yourdomain.com", "fromEmailPassword");
smtp.Timeout = 20000;
}
// Now send your email with this smtp
smtp.Send(message);
Optionally you can also try to enable/disable ssl & changing the port.
Related
The below code is working fine from Windows Form, but not working from Windows Services.
The service is running on Windows XP.
I have tried changing the log on user, but did not work.
Error: Transaction failed. the server response was 5.7.1 client host
rejected access denied
private void SendEmailToHO()
{
try
{
int mailSentSuccessfully = 0;
MailAddress to = new MailAddress(mailTo);
MailAddress from = new MailAddress(mailFrom);
using (MailMessage mail = new MailMessage(from.Address, to.Address))
{
int attachmentCount = 0;
try
{
foreach (string fileName in fileEntries)
{
Attachment attachment = new Attachment(fileName);
mail.Attachments.Add(attachment);
attachmentCount++;
}
SmtpClient client = new SmtpClient(mailHost, port);
if (enableSSL == "Y")
{
client.EnableSsl = true;
}
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(mailUser, mailPassword);
mail.Subject = "Email Subject " + clientID;
mail.Body = "Attached please find the files: " + clientIDTitle;
if (attachmentCount > 0)
{
//
client.Send(mail);
//if no error, this code will work.
mailSentSuccessfully = 1;
new MyApp.LogWriter("Sent mail to " + to.Address + ", \nAttachment count = " + attachmentCount);
}
else
{
new MyApp.LogWriter("Attachment count = " + attachmentCount);
}
}
catch (Exception ex)
{
new MyApp.LogWriter("Send mail failed. Cause: " + ex.Message
+ "\n Inner Exception: " + ex.InnerException);
}
}
}
catch (System.Exception ex)
{
new BTCClient.LogWriter("Email Error '" +
ex.Message + "'");
}
}
I was facing the same issue, To resolve this issue you need to contact your email server's service provider and tell them to give access to your server IP.
im using Asp.net And I have the Umbraco Cms on my project
i implemented The Send message using razor syntax
im submitting a from to the following razor syntax
#{ if (IsPost)
{
string name = Request["name"];
string email = Request["email"];
string phone = Request["phone"];
string city = Request["city"];
string note = Request["note"];
NetworkCredential basicCredential =new NetworkCredential("*****#gmail.com", "******");
MailMessage mail = new MailMessage();
//var from = new MailAddress(Email.Text);
mail.From = new MailAddress(email);
mail.To.Add("tupacmuhammad5#gmail.com");
mail.Subject = "Torcan WebSite Contact Us";
mail.IsBodyHtml = false;
mail.Body = "You just got a contact email:\n" +
"Name: " + name + "\n"
+ "Email: " + email + "\n"
+ "TelePhone: " + phone + "\n"
+ "Address: " + city + "\n"
+ "Message: " + note + "\n";
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.gmail.com";
smtp.Credentials = basicCredential;
smtp.EnableSsl = true;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
finally
{
smtp.Dispose();
}
}
}
it works perfect on My localHost But on a live server its thorws a runtime error "after i remove the try catch "
i cant find out what seems to be problem
im writing this code in umbraco backoffice tamplate i have the server on onther country and i dont have access to it any help ? please ?
The Problem was that the Server is located in Germany so when the server tries to open my Gmail account. its a different country and never seen action from there.
so all i had to do is to open my gmail via remote access on the server machine via a browser and confirmed it was me then every thing worked perfectly.
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);
}
I am trying to create a facebook share button inside to an email and share the attached video via C#.
First of all, I have attached into the email the video which i am trying to send.
But i can't create via C# the facebook button that i want to share the video.
You may find my source code so far :
public static string logopath = Path.Combine(Application.StartupPath, "logo.png");
public static string videopath = Path.Combine(Application.StartupPath, "test.mp4");
private void button1_Click_1(object sender, EventArgs e)
{
string usermail = textBox1.Text;
bool isUsermailNull = usermail.Equals("");
Console.Out.WriteLine("The variable is " + isUsermailNull);
if (isUsermailNull.Equals(false))
{
try
{
MailAddress m = new MailAddress(usermail);
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("myemailaccount#gmail.com");
message.To.Add(new MailAddress(usermail));
message.Subject = "SUBJECT";
message.IsBodyHtml = true;
Attachment inlineLogo = new Attachment(logopath);
Attachment inlineLogo2 = new Attachment(videopath);
message.Attachments.Add(inlineLogo);
message.Attachments.Add(inlineLogo2);
string contentID = "Image";
string contentID2 = "Image2";
inlineLogo.ContentId = contentID;
inlineLogo2.ContentId = contentID2;
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inlineLogo2.ContentDisposition.Inline = true;
inlineLogo2.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//To embed image in email
//string pass = "123456";
message.Body = "<html><body> <h1 align=\"center\"><img src=\"cid:" + contentID + "\"> </h1><video width=\"400\" controls><source src=\"cid:" + contentID2 + "\" type=\"video/mp4\">Your browser does not support HTML5 video.</video> <h1 align=\"center\">Project</h1> "
+ " <h2 align=\"center\"> NameofProject </h2><h2 align=\"center\">You may find attached your video</h2><div id=\"fb-root\"></div><script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = \"//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7\"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <div class=\"fb-video\" data-href=\"cid:" + contentID2 + "\" data-width=\"500\" data-show-text=\"false\"><div class=\"fb-xfbml-parse-ignore\"><blockquote cite=\"cid:" + contentID2 + "\">MyProject </blockquote> </div> </div> <h3 align=\"center\"> Thank you</h3> <h2 align=\"center\">Details</h2> <h2 align=\"center\"> Mail us : myemailaccount#gmail.com</h2> </body></html>";
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("myemailaccount#gmail.com", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Timeout = 5000000;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
this.Close();
}
catch (FormatException)
{
MessageBox.Show("You haven't put correct email address. Try again!!!!");
}
//Kill all on screen keyboards
Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
foreach (Process onscreenProcess in oskProcessArray)
{
onscreenProcess.Kill();
}
}
else
{
MessageBox.Show("The textbox is null. Please insert an appropriate email address");
}
}
Thank you in advance
You need add in body a hmtl <a> with a url that contains your file like for example:
Share in Facebook
Intro: I am supporting a piece of code that sends a welcome/confirm mail upon successful registration to a site.
The SMTP client times out before sending can complete. According to our Ops department, I am using the correct credentials and SMTP server IP address.
What I want to do is hook into any handshaking, connection, sending, etc events so that I can figure out exactly why the process times out. I've set Server.ScriptTimeOut to 500 seconds on that page, so it can't just be a busy server problem.
My question is this: what events can I handle during this process. The only one I see exposed in SMTPClient is SendCompleted. But I imagine there must be a way to get access to something more low-level?
For reference, here's the code:
//Create and send email
MailMessage mm = new MailMessage();
try
{
mm.From = new MailAddress("admin#site.com");
mm.To.Add(new MailAddress(Recipient));
mm.Subject = Subject;
mm.Body = MailBody;
mm.IsBodyHtml = true;
var c = new NetworkCredential("admin#site.com", "YeOlePassword");
SmtpClient smtp = new SmtpClient("127.0.0.1");//not really, just hiding our SMTP IP from StackOverflow
smtp.UseDefaultCredentials = false;
smtp.Credentials = c;
smtp.Send(mm);
}
catch (Exception x)
{
DateTime CurrentDateTime = DateTime.Now;
String appDateTime = CurrentDateTime.ToString();
String transactionLogEntry = "To: " + Recipient + " " + appDateTime + " " + x.Message;
StreamWriter streamwriter = new StreamWriter(File.Open(MapPath("~/TransactionLog.txt"), FileMode.Append, FileAccess.Write, FileShare.Write));
//Append to file
streamwriter.WriteLine(transactionLogEntry);
//Close file
streamwriter.Close();
}
The error that's logged is simply that a timeout occured while sending a mail to the supplied email address.
I use this to get the buried SMTP messages out.
String transactionLogEntry = "To: " + Recipient + " " + appDateTime + " " + GetFullErrorMessage(x);
.
.
.
private string GetFullErrorMessage(Exception e)
{
string strErrorMessage = "";
Exception excpCurrentException = e;
while (excpCurrentException != null)
{
strErrorMessage += "\"" + excpCurrentException.Message + "\"";
excpCurrentException = excpCurrentException.InnerException;
if (excpCurrentException != null)
{
strErrorMessage += "->";
}
}
strErrorMessage = strErrorMessage.Replace("\n", "");
return strErrorMessage;
}
In my experience, from what you are describing it is related to your SMTP port being blocked by and ISP or your server/web host. Good luck!