Send email form C#

  • Thread starter Abhishek Srivastava
  • Start date
A

Abhishek Srivastava

Hello All,

I wrote the follownig program

Code:
using System;
using System.Web.Mail;
public class MailHelper
{
public static void SendMail(string mailId, string author, string email,
string title, string msgDate, string comment)
{
String msg = String.Format("{0} (email: {1}) has just posted a comment
on the message \"{2}\" of your BLOG that you posted at {3}. Here's the
comment: {4}{4}{5}",
author,
email,
title,
msgDate,
Environment.NewLine,
comment
);
try
{
//SmtpMail.SmtpServer = "localhost";
MailMessage mail = new MailMessage();
mail.To = mailId;
mail.From = "[email protected]";
mail.Subject = "New BLOG comment notification";
mail.Body = msg;
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("*****************");
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine("*****************");
Console.WriteLine(ex.InnerException.InnerException.Message);
throw ex;
}
}
}

The code doesn't throw any exception ... but it doesn't send the mail as
well.

I have configured the SMTP service on my local machine itself. I have
also gone into SMTP server properties and into the "Access->Relay"
option and added my machine IP (127.0.0.1 and my ip address) into the
list of "only the list below" option.

Please do help me... why am I not able to send the email?

regards,
Abhishek.
 
P

Patrick Schwegler

Abhishek Srivastava wrote:
[cut]
Please do help me... why am I not able to send the email?

regards,
Abhishek.

Hi,
Try to uncomment
Code:
SmtpMail.SmtpServer = "localhost";


Greets
Patrick
 
A

Abhishek Srivastava

If I do that I get the exception

System.Web.HttpException: Could not access 'CDO.Message' object. --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.InteropServices.COMException (0x80040213): The transport
failed to connect to the server.

--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags
invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[]
namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type,
Object obj, String methodName, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj,
String methodName, Object[] args)
--- End of inner exception stack trace ---
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj,
String methodName, Object[] args)
at System.Web.Mail.CdoSysHelper.Send(MailMessage message)
at System.Web.Mail.SmtpMail.Send(MailMessage message)
at BLogComponents.MailHelper.SendMail(String mailId, String author,
String email, String title, String msgDate, String comment) in
c:\myapps\dotnet\blog\components\mailhelper.cs:line 25
*****************
Exception has been thrown by the target of an invocation.
*****************
The transport failed to connect to the server.


Patrick said:
Abhishek Srivastava wrote:
[cut]
Please do help me... why am I not able to send the email?

regards,
Abhishek.

Hi,
Try to uncomment
Code:
SmtpMail.SmtpServer = "localhost";


Greets
Patrick
 
G

Girish Bharadwaj

Abhishek said:
If I do that I get the exception

System.Web.HttpException: Could not access 'CDO.Message' object. --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Runtime.InteropServices.COMException (0x80040213): The transport
failed to connect to the server.

--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags
invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[]
namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object
obj, String methodName, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj,
String methodName, Object[] args)
--- End of inner exception stack trace ---
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj,
String methodName, Object[] args)
at System.Web.Mail.CdoSysHelper.Send(MailMessage message)
at System.Web.Mail.SmtpMail.Send(MailMessage message)
at BLogComponents.MailHelper.SendMail(String mailId, String author,
String email, String title, String msgDate, String comment) in
c:\myapps\dotnet\blog\components\mailhelper.cs:line 25
*****************
Exception has been thrown by the target of an invocation.
*****************
The transport failed to connect to the server.


Patrick said:
Abhishek Srivastava wrote:
[cut]
Please do help me... why am I not able to send the email?

regards,
Abhishek.

Hi,
Try to uncomment
Code:
SmtpMail.SmtpServer = "localhost";


Greets
Patrick

It almost sounds like your SMTP service is either not running or not
accepting calls. Try going to Outlook Express and set the SMTP server to
be localhost and try sending an email. Also, check the BadMail Folder
under mailroot directory. You might get some insight.
 
C

Cybertof

Is it the only to send an Email using CDO ?
In the past, CDO was meaning of having at least a ms-exchange client on
the pc (like outlook), and also a correct profile.

Is there another way to send an email more directly ? a SMTP Class ?

Regards,
Cybertof.
 
S

Steven

You will need outlook or outlook express to use the SMTPMail Class as it
utilizies the CDO object.

If you don't want to rely on CDO, you will need to write a class that can
connect to an SMTP server and execute the neccessary commands.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top