.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Sending Mail

Posted By: R@j     Posted Date: March 08, 2009    Points:2   Category :ASP.Net
Hi Some one Plz help me. I got error while sending mail from code.
My error goes like

System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: POP3 at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) at ........


My code goes like

String tt_to, tt_from, tt_subject, tt_body;
tt_to = "xxxxxxxx@gmail.com";
tt_from = "xxxxxx@domain.com";
tt_subject = "Mail from .Net code";
tt_body = "Sample mail";
try
{


System.Net.Mail.MailMessage mailmsg = new MailMessage(tt_from, tt_to, tt_subject, tt_body);
mailmsg.Priority = MailPriority.High;
mailmsg.IsBodyHtml = false;


System.Net.Mail.SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.domain.com";
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.EnableSsl = false;
smtp.Port = 110;
smtp.Send(mailmsg);
Response.Write("Mail Sent Successfully");
}
catch (Exception ex)
{
Response.Write("Send failure: " + ex.ToString());
}

I configured Outlook with the same settings as above(server,port,uname,pwd....).It works fine. But its getting the above exception in code

Plz help me.Its very Urgent.

THANKS in Advance

Responses
Author: ranga             
Posted Date: March 08, 2009     Points: 5   

hi,
i think there is a problem in using NameSpaces.
I think u missed some namespace to add.
use the following code, defenetly it 'll help u
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

MailMessage email = new MailMessage(txtFrom.Text, txtTo.Text, txtSub.Text, txtMsg.Text);
SmtpClient mailClient = new SmtpClient();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);
mailClient.Host = settings.Smtp.Network.Host;
mailClient.Port = settings.Smtp.Network.Port;
mailClient.Credentials = cred;
mailClient.Send(email);
lblError.Text = "message sent successfully";

}

}

now in your web.config write the following tag under configuration section

<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="optional">
<network defaultCredentials="true" port="ur port number" host="ur smtp server name(hosting site name)" password="xxx" userName="username"/>
</smtp>
</mailSettings>
</system.net>

Author: Athira Sreejith             
Posted Date: March 11, 2009     Points: 5   

Hi..

Refer this code:

private void SendMail()
{
try
{
MailMessage m_newmail = new MailMessage();
SmtpClient s_client = new SmtpClient();
string attachfile=string.Empty;
Attachment m_attach;
string attachfile1=string.Empty;
string strdir = string.Empty;
MailAddress fromAdr = new MailAddress(txtEmail.Text);
m_newmail.From = fromAdr;

string strMailId = System.Configuration.ConfigurationManager.AppSettings["MailId"].ToString();
m_newmail.To.Add(strMailId);
m_newmail.Subject = "About Enquiry";

StringBuilder MailBody = new StringBuilder();
MailBody.Append("Name :" + txtName.Text.Trim() + "<br/>");
MailBody.Append("Designation :" + txtDesgntn.Text + "<br/>");
MailBody.Append("Company Name :" + txtCoName.Text + "<br/>");
MailBody.Append("Address :" + txtAddress.Text + "<br/>");
MailBody.Append("Pin :" + txtPin.Text + "<br/>");
MailBody.Append("Country :" + txtCountry.Text + "<br/>");
MailBody.Append("Telephone :" + txtTel1.Text + "-" + txtTel2.Text + "-" + txtTel3.Text + "<br/>");
MailBody.Append("Fax :" + txtFax.Text + "<br/>");
MailBody.Append("Like to comment on :" + cmbComment.SelectedValue + "<br/>");
MailBody.Append("Suggestions :" + txtSuggestions.Text + "<br/>");
MailBody.Append("Please tell us what you are looking for in enquiry :" + chklstEnquiry.SelectedValue + "<br/>");
MailBody.Append("Additional Information :" + txtAdditionalInfo.Text + "<br/>");
MailBody.Append("How did you here about us :" + cmbSelect.SelectedValue + " <br/>");

m_newmail.Body = MailBody.ToString();
m_newmail.IsBodyHtml = true;

if(fileName.HasFile)
{
attachfile = Path.GetFileName(fileName.PostedFile.FileName);
fileName.PostedFile.SaveAs(Server.MapPath(attachfile));

m_attach = new Attachment(Server.MapPath(attachfile));

m_newmail.Attachments.Add(m_attach);
attachfile1 = attachfile;
}
s_client.Host = "localhost";
s_client.UseDefaultCredentials = true;
s_client.Send(m_newmail);

ClientScript.RegisterClientScriptBlock(this.GetType(), "Hi!", String.Format("alert('An email has successfully been sent to {0}');", m_newmail.To), true);
ClearAll();

m_newmail.Dispose();

if (attachfile1 != null)
{
File.Delete(Server.MapPath(attachfile1));
}
}
catch (Exception ex)
{
ClearAll();
}
}

Author: krishana singh             
Posted Date: October 16, 2010     Points: 5   

try
{
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
string insertcontact = "INSERT INTO tblContactUs(Uname,Address,Mobile,Email,Comment) VALUES('" + txtname.Text + "','" + txtadd.Text + "','" + txtmobile.Text + "','" + txtemail.Text + "','" + txtcomment.Text + "')";
SqlCommand cmd = new SqlCommand(insertcontact,con);
cmd.ExecuteNonQuery();
txtadd.Text = txtemail.Text = txtemail.Text = txtcomment.Text = txtname.Text = txtmobile.Text = "";

string bodytext = "<html>";
bodytext += "<table border='1'>";
bodytext += "<tr><td>Name: </td><td>'" + txtname.Text + "'</td></tr><br/>";
bodytext += "<tr><td>EmailID: </td><td>'" + txtemail.Text + "'</td></tr><br/>";
bodytext += "<tr><td>Mobile:</td><td>'" + txtmobile.Text + "'</td></tr><br/>";
bodytext += "<tr><td>Address:</td><td>'" + txtadd.Text + "'</td></tr><br/>";
bodytext += "<tr><td>Comment</td><td>'" + txtcomment.Text + "'</td></tr><br/>";
bodytext += "</table>";
bodytext += "</html>";
Email.SendMail(txtemail.Text, "info@theflowersworld.com", "Contact information", bodytext);
Email.SendMail("info@theflowersworld.com", txtemail.Text, "confirmation mail", "your information saved successfully ");
}
catch (Exception ex)
{

txtadd.Text = txtemail.Text = txtemail.Text = txtcomment.Text = txtname.Text = txtmobile.Text = "";
}
}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend