Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to send email using asp.net and c#, but it shows failure sending email.
i have the domain with .ca

Does it matter.

Thanks
Naveen
Posted
Comments
Kornfeld Eliyahu Peter 29-Jan-14 15:16pm    
Domain does not matter - show your code and may someone find you a solution...
Matt T Heffron 29-Jan-14 19:17pm    
Unless you really like SPAM, you should change your account name NOT to be your email address!
The email address harvesters used by spammers look EVERYWHERE!

VB
Public Function SendMail(sender As Object, e As EventArgs) As String
        Dim retVal As String = Nothing
        Dim urlURI As String = HttpContext.Current.Request.Url.AbsoluteUri
        Dim urlPath As String = HttpContext.Current.Request.Url.AbsolutePath
        Dim myServerName As String = Strings.Left(urlURI, urlURI.Length - urlPath.Length)
        Dim clientMail As String = ' This is your client e-mail 
        Dim myCred As Net.NetworkCredential = New Net.NetworkCredential
        Dim MailConf As New Net.Mail.SmtpPermissionAttribute(System.Security.Permissions.SecurityAction.Assert)
        myCred.Domain = 'here is you mail Domain... your domain
        myCred.Password = ' here is the password from the above account
        myCred.UserName = ' here is an account on your mail server on which you'll Logged in
        Dim myGuid As String = 'If you want to reply in this mail then put his Guid record from SQL server
        'Create a new MailMessage object and specify the"From" and "To" addresses
        Dim Email As New System.Net.Mail.MailMessage(myCred.UserName, clientMail)
        Email.Subject = "Verification Mail from ......."
        Dim messageString As String = 'Your Message string on the e-mail body

        Email.Body = messageString
        Dim mailClient As New System.Net.Mail.SmtpClient()
        'This object stores the authentication values
        Dim basicAuthenticationInfo As _
           New System.Net.NetworkCredential(myCred.UserName, myCred.Password)
        mailClient.Host = myCred.Domain
        mailClient.UseDefaultCredentials = False
        mailClient.Credentials = basicAuthenticationInfo
        mailClient.Send(Email)
        reader.Dispose()

        Return retVal
    End Function


Well that's my propose in your issue with sending e-mail
 
Share this answer
 
You can use a gmail acount for that purpose
Send parameters for the message you want to send and the e-mail address for the receiver of the e-mail to a void method

C#
private void SendNotification ( string message, string reciever )
    {
   
    string userName = "yourName@gmail.com"; //insert the usernme for the mail account
    string passWord = "yourPassword"; //insert corrosponding password
    string fromText = "DoNotReply@domain.com"; //Whatever to identify the Sender
    
    string toText = receiver; 
    string subjectText = "e-mail header text";
    string bodyText = "email body text"

    SmtpClient smtpClient = new SmtpClient ( "smtp.gmail.com", 587 );
    smtpClient.EnableSsl = true;
    smtpClient.Timeout = 10000;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential ( userName, passWord );
    MailMessage mail = new MailMessage ( fromText, toText, subjectText, bodyText );
    mail.BodyEncoding = UTF8Encoding.UTF8;
    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    smtpClient.Send ( mail );
}        
 
Share this answer
 
Try this code:

you have to use SMTP service like GMAIL/YAHOO.

C#
public void GetMail()
       {
           NetworkCredential cred = new NetworkCredential("name@domail.com", "Password");
           MailMessage msg = new MailMessage();
           msg.To.Add("name@domail.com");
           msg.Subject = "Welcome";

           msg.Body = "You Have Successfully sent mail!!!";
           msg.From = new MailAddress("name@domail.com"); // Your Email Id
           SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
           SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
           client.Credentials = cred;
           client.EnableSsl = true;
           client.Send(msg);
       }
 
Share this answer
 
v2
// use namespace "using System.Net.Mail;"

      protected string SendEmail(string toAddress, string subject, string body)
      {
          string result = "Message Sent Successfully..!!";
          string senderID = "SenderEmailID";// use sender's email id here..
          const string senderPassword = "Password"; // sender password here...
          try
          {
              SmtpClient smtp = new SmtpClient
              {
                  Host = "smtp.gmail.com", // smtp server address here...
                  Port = 587,
                  EnableSsl = true,
                  DeliveryMethod = SmtpDeliveryMethod.Network,
                  Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                  Timeout = 30000,
              };
              MailMessage message = new MailMessage(senderID, toAddress, subject, body);
              smtp.Send(message);
          }
          catch (Exception ex)
          {
              result = "Error sending email.!!!";
          }
          return result;
      }
 
Share this answer
 
One simple way is you can use already available SP in Sql server
SQL
Create Procedure sp_SMTPMail @SenderName varchar(100), @SenderAddress varchar(100), @RecipientName varchar(100), @RecipientAddress varchar(100), @Subject varchar(200), @Body varchar(8000), @MailServer varchar(100) = 'localhost' AS	 SET nocount on 


and later call the stored procedure in DAL layer of asp.net

or the other way you can refer the below code.

C#
  protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = txtFrom.Text;
// Recipient e-mail address.
Msg.To = txtTo.Text;
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "10.20.72.1";
SmtpMail.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}


Regards,
Praveen Nelge</img>
 
Share this answer
 
v3
Comments
[no name] 31-Jan-14 1:20am    
Thanks Jas
Tom Marvolo Riddle 31-Jan-14 1:50am    
It's ok Praveen
using System.Net.Mail;
using System.IO;

{
MailMessage mail = new MailMessage();
mail.To.Add(TextBox1.Text.ToString());
mail.From = new System.Net.Mail.MailAddress(TextBox2.Text.ToString());
mail.Subject = TextBox3.Text.ToString();
mail.Body = TextBox4.Text.ToString();
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smp = new SmtpClient("Smtp.gmail.com");
smp.Credentials = new System.Net.NetworkCredential(TextBox2.Text.ToString(), TextBox3.Text.ToString());
smp.Port = 589;
smp.Host = "smtp.gmail.com";
smp.EnableSsl = true;
smp.Send(mail);
Label1.Text = "Send message Succesfully";
}
 
Share this answer
 
Thank you for your immediate replies: But i have my own email server with domain name. Gmail email sending is not what I am trying for.

Below is my code i am testing.

I can access directly to webmail.domain.ca

I tried in ASP not ASP.NET. But in that it is sending email@domain.ca to email@domain.ca with the information in the form.Thats not what i am trying for.

What i am doing is a feedback form.The user will enter his/her Name,Ph,Emailid and message.
I need the the entered emailid(user's) to send me the message to the email@domain.ca.

So in email@domin.ca inbox it will be seen as sender is the emailid of the user.


//**************************************************//
public void sendit(string name, string phone, string email, string mess)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(email);
message.From = fromAddress;
message.To.Add("email@domain.ca");
//if (ccList != null && ccList != string.Empty)
// message.CC.Add(ccList);
message.Subject = "Enquiry";
message.IsBodyHtml = true;
message.Body = mess;
smtpClient.Host = "webmail.domain.ca";
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = true;// Here I tried true as well as false
smtpClient.Credentials = new System.Net.NetworkCredential("email@domain.ca","pass");

smtpClient.Send(message);
msg = "Successful";
}
catch (Exception ex)
{
msg = ex.Message;
}
//return msg;


}

//**************************************************//

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900