Click to See Complete Forum and Search --> : [RESOLVED] Problem sending an E-mail


vuyiswam
June 19th, 2009, 05:20 AM
Good Morning All

I have the Following code that sends an e-mail, let me first post the markup and the code behind. The following is the markup of my page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
To &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:TextBox ID="txtto" runat="server" Width="325px"></asp:TextBox><br />
<br />
From &nbsp;&nbsp; &nbsp;<asp:TextBox ID="txtFrom" runat="server" Width="326px"></asp:TextBox><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
<br />
CC: &nbsp; &nbsp;&nbsp; &nbsp;<asp:TextBox ID="txtcc" runat="server" Width="326px"></asp:TextBox><br />
<br />
Subject:
<asp:TextBox ID="txtsubject" runat="server" Width="326px"></asp:TextBox><br />
Body :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
<asp:TextBox ID="txtbody" runat="server" Height="86px" Width="314px"></asp:TextBox><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
Port: &nbsp; &nbsp; &nbsp; &nbsp;
<asp:TextBox ID="txtport" runat="server" Width="127px"></asp:TextBox><br />
&nbsp;<br />
Host &nbsp; &nbsp; &nbsp; &nbsp;
<asp:TextBox ID="txthost" runat="server" Width="127px"></asp:TextBox><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:Button ID="btnSend" runat="server"
OnClick="btnSend_Click" Text="Send" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" /><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>


and i have a Function that sends the e-mail like this


private static string SendEmail(String from, string to, string cc,int port, string subject, string message,String Host)
{
//create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//set the content
mail.From = new System.Net.Mail.MailAddress(from,"Test E-mail");
mail.To.Add(new System.Net.Mail.MailAddress(to, "Test E-mail"));
mail.Subject = subject;

mail.Body = message;

mail.IsBodyHtml = false;


// seems we can use either the big routing server or our own ip address
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

smtp.Port = port;

smtp.Host = Host;
//mailSenderInstance.Credentials = New System.Net.NetworkCredential("LoginAccout", "Password")
smtp.Credentials = new System.Net.NetworkCredential("administrator", "skskssk");

try
{
smtp.Send(mail);
return "Mail sent successfully!";
}
catch (Exception ex)
{
Exception ex2 = ex;

string errorMessage = string.Empty;

while (ex2 != null)
{
errorMessage += ex2.ToString();

ex2 = ex2.InnerException;
}

return errorMessage;
}
// Mailbox unavailable. The server response was 5.7.1. Unable to relay for (name).
// see notes under Techdocs
//client.Host = "localhost"; // setup the server / host sending the mail
// client.UseDefaultCredentials = true;

}


and in my button send we have the following


protected void btnSend_Click(object sender, EventArgs e)
{
Label1.Text = SendEmail(txtFrom.Text, txtto.Text, txtcc.Text, Convert.ToInt32(txtport.Text), txtsubject.Text,txtbody.Text,txthost.Text);

}



I have entered the Following in the Required Fields when i test the application


To: eddf@fdff.co.za

From:dfd@dfdf.co.za

CC:drfd@ddtef.com

Subject: This is the Test for the mail in o!booking System

BodyIf you Receive this e-mail Please confirm the receipt of this e-mail

Port : 25

Host: mailserver.drfgd.local




When i run my Application and clicking on the send button , i get the Following Error


System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: Microsoft Exchange Server 2003 POP3 server version 6.5.7638.1 (Mailserver.its.local) ready. 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 _Default.SendEmail(String from, String to, String cc, Int32 port, String subject, String message, String Host) in c:\Vuyiswa\Email_test\Default.aspx.cs:line 53


Thanks

Mike Pliam
June 20th, 2009, 11:16 AM
There's alot of different syntax posted out there. I had similar problems. I'm not sure, but I think your problem is in your sendmail syntax somewhere.

This worked for me with a Label (Label1), From (FROM), To(TO), Subject (SUBJECT), and BodyText (BODYTEXT) text boxes on the page

using System.Net.Mail;

public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Please enter your name and email address below if you wish a response.";
}
protected void SendMail()
{
string sSender;
sSender = NAME.Text + "<" + EMAIL.Text + ">";

try
{
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress("sender@senderdomain.com");
oMsg.To.Add("recipient@recipientdomain.com");
oMsg.Headers.Add("ReplyTo", sSender);
oMsg.Subject = SUBJECT.Text;
oMsg.Body = BODYTEXT.Text;
SmtpClient smtp = new SmtpClient("scriptmail.intermedia.net");
smtp.Send(oMsg);
oMsg = null;
}
catch (Exception e)
{
Label1.Text = "Exception caught." + e.ToString();
}


scriptmail.intermedia.net is my server mailer reference. The poorly documented oMsg.Headers.Add("ReplyTo", sSender) provides the recipient with name and email of the sender. I'm a bit unsure of the "ReplyTo" syntax so look it up if you want to use it.

Hope this helps.

Mike

vuyiswam
June 22nd, 2009, 02:19 AM
I think i have internal Problems. The Port that hasbeen suplied to me is incoreect or the Credentials are not Correct. i have tried the following code that uses gmail and works Perfectly.


protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mm = new MailMessage();

SmtpClient smtp = new SmtpClient();

mm.From = new MailAddress("Vuyiswa@its.co.za");

mm.To.Add(new MailAddress("Vuyiswa@its.co.za"));

mm.To.Add(new MailAddress("Vuyiswamb@webmail.co.za"));

mm.Subject = "Hello";

mm.Body = "<p>This is the E-mail Test for !Booking System</p>";

mm.IsBodyHtml = true;

smtp.Host = "smtp.gmail.com";

smtp.EnableSsl = true;

System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();

NetworkCred.UserName = "vuyiswamb@gmail.com";

NetworkCred.Password = "secret";

smtp.UseDefaultCredentials = true;

smtp.Credentials = NetworkCred;

smtp.Port = 587;

smtp.Send(mm);
}