Answered by:
Error Sending Mail Through Asp.net C#

Question
-
User1120627064 posted
hi in my asp.net apllication(c#) am working with sending mail issue..
my following below code is
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage ();
msg.From =smitha@gmail.com;
msg.To = shruthi@gmail.com
msg.Subject = sub;
msg.Body = body;
System.Web.Mail.SmtpMail.SmtpServer = "mywebsite.com";
System.Web.Mail.SmtpMail.Send(msg);
am getting a error message like"Could not create an object of type 'CDO.Message'. Please verify that the current platform configuration supports SMTP mail."..
please suggest me the solutionTuesday, September 9, 2008 2:10 AM
Answers
-
User1120627064 posted
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Net.Mail;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body"));
}
public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "mail.server.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");
smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 11, 2008 9:12 AM
All replies
-
User-419173150 posted
I believe SMTP Service is installed and running properly.
You may refer http://support.microsoft.com/kb/910360
Tuesday, September 9, 2008 5:49 AM -
User1120627064 posted
Sir Hemantksh
Could u please tell me is tere any error in the above code
i read that article but now microsoft doen't support CDO so what i can i do to send mails through my application... please suggest.
Tuesday, September 9, 2008 6:18 AM -
User142102346 posted
Please try this out...and let me know if u have any errorsstring emailTo = "info@server.com"; _emailFrom = Request.Form["tbEmail"]; _subject = Request.Form["tbSubject"]; _message = Request.Form["tbMessage"]; System.Net.Mail.MailMessage objEmail = new System.Net.Mail.MailMessage(_emailFrom, emailTo, _subject, _message); try{ System.Net.Mail.SmtpClient n = new SmtpClient("mail.sever.com"); n.Credentials = new NetworkCredential("info@sever.com","Password"); n.Send(objEmail); Response.Write("Your Email has been sent sucessfully - Thank You"); } catch (Exception exc){ Response.Write("Send failure: " + exc.ToString()); }
Tuesday, September 9, 2008 6:27 AM -
User1120627064 posted
Sir i am not able to execute the code the error before running the code is
"The type or namespace name 'NetworkCredential' could not be found (are you missing a using directive or an assembly reference?) "
Tuesday, September 9, 2008 7:11 AM -
User-419173150 posted
Try using System.Net.NetworkCredential
Tuesday, September 9, 2008 7:21 AM -
User1120627064 posted
Now The Error Changes to
"A using namespace directive can only be applied to namespaces; 'System.Net.NetworkCredential' is a type not a namespace"
Tuesday, September 9, 2008 7:29 AM -
User1433122370 posted
hi Use this
using System.Net;
using System.Net.Mail;
using System.Web.Mail;const string SERVER = "relay-hosting.secureserver.net";
System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
objMail.From = new MailAddress("test@gmail.com");
objMail.To.Add(objRegUsers.EmailId);
objMail.IsBodyHtml = true;
objMail.Subject = "New Account Confirmation";
objMail.Priority = System.Net.Mail.MailPriority.High;
string l_body;
l_body = "<br/>" + "This";
objMail.Body = l_body;
SmtpClient Client = new SmtpClient(SERVER);
Client.Send(objMail);
Tuesday, September 9, 2008 7:32 AM -
User1120627064 posted
While using the above code
D error at Client.Send(objMail);
is
'Mailbox name not allowed. The server response was: sorry, relaying denied from your location [59.95.180.74] (#5.7.1)"
Tuesday, September 9, 2008 8:00 AM -
User142102346 posted
May you please send the code snippet that you are using?
Tuesday, September 9, 2008 8:14 AM -
User-1936035690 posted
Hi,
This error:
"'Mailbox name not allowed. The server response was: sorry, relaying denied from your location [59.95.180.74] (#5.7.1)" "
Actually tells us your code is working. That reply is coming back from the relay server. The mail server isn't allowing relaying from your IP of 59.95.180.74. Try specifing a username and password. If that doesn't work, you will need to get with your admin to get the correct information for programmatically sending emails.
Cheers!
DaveTuesday, September 9, 2008 2:43 PM -
User-1763611275 posted
Hi
for sending email
try this example
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Web.Mail;
public partial class SendEmail : System.Web.UI.Page
{
string strMailOrTel;
protected void Page_Load(object sender, EventArgs e)
{
}
private bool SendMail()
{
try
{
string strTo = "your email";
string strFrom = txtFrom.Text;
string strBody = txtMessage.Text ;
string strSubject = txtSubject.Text ;
SmtpMail.SmtpServer = ("your smtp");
SmtpMail.Send(strFrom, strTo, strSubject, strBody);
return true;
}
catch (Exception ex)
{
return false;
}
}
protected void btn_Send_Click(object sender, EventArgs e)
{
bool TrueOrFalse = SendMail();
if ((TrueOrFalse == true))
{
Response.Redirect("~/MailSent.aspx");
}
else
{
Label1.Text = "Try again";
}
}
}or
check these links for sending mail
1. http://www.developer.com/net/asp/article.php/3096831
2.Using ASP.NET to Send E-Mail—Including Attachments - http://www.developer.com/net/asp/article.php/3287361
3.http://forums.asp.net/t/1269838.aspx
4.How to configure an Internet Information Services SMTP virtual server to archive or to remove messages in an Exchange Server 2003 test environment
http://support.microsoft.com/kb/837851
5.Sending HTML Mail with Embedded Image in .NET
http://aspalliance.com/1354_Sending_HTML_Mail_with _Embedded_Image_in_NET.all
Good Luck
Tuesday, September 9, 2008 7:00 PM -
User1120627064 posted
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Net.Mail;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write( SendMail("meetuchoudhary@gmail.com","meetudmeet@gmail.com","meetudmeet@yahoo.com","Test Mail","Test Mail Body"));
}
public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "mail.server.com";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");
smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 11, 2008 9:12 AM