Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm really struggling to get this one right. I want my button to SAVE the data entered on a webform into the database AND that same button should send a notification mail as well.
This is the basic example of what I've got:
C#
private void button()
{
     Save();
     SendEmail();
}
private void SendEmail()
{
     //code here;
}
private void Save()
{
 //code here;

}

Okay, now the first part works perfectly fine, and that's the SAVE method(). The SendEMail part is where the struggling starts.

First off. A name in a drop down list is selected (this name corresponds with that person's email address). then the rest is filled in.
Example of a database:
ID      Name      Email
0       aaa       aaa@a.com


So once the dropdownlist is selected, is should select the email address. This part works fine for me too.

Now, when I press on the submit button which Saves data and sends the notification to the person selected, it gives me an SMTP Exception. This is my SendEmail() code:

C#
     MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
    MailAddress fromAddress = new MailAddress(from);
    message.From = fromAddress;


    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";


I am pretty sure some setting or something is still not right because I've tried multiple lines of different code to make sure the emails are working, but still get that exception. Sometimes it gives a Time Out Exception.

Yeah, so can anybody help me please.
Posted
Updated 2-Jul-14 3:50am
v2
Comments
[no name] 2-Jul-14 10:19am    
What is the exception? That should tell you right there what the prolem is. Do you have an email account for "mail.server.com"? Is the port they are really using 25? Did you ask the admin what the setup is? Is the user name and password correct?
Member 10834062 2-Jul-14 10:35am    
Really?
I get Time out Exception. --> Doesn't tell me much. I used port 25 for a test once and got a STMP Exception, so I've changed it to the correct one and get a Time-out exception after a while.
As for the rest of your questions.. The details provided here are obviously fake. I am the admin. And the username and password is correct.
[no name] 2-Jul-14 10:43am    
Really? yourself... How would expect any of us to know what the exception is? Or that you are the admin? Or that the information that you give us is totally fake?
Member 10834062 2-Jul-14 10:52am    
If you read it again, I have stated the two exceptions. And I just thought mail.server.com would be a nice example. Peace

 
Share this answer
 
Comments
Jαved 2-Jul-14 11:55am    
Welcome bro :)
Hi,

Possibilities :
1. I may be a case of SSL.

You could try :
C#
smtpClient.EnableSSL=true;


2. Check port and IP is accessible from the client pc /server
 
Share this answer
 
Comments
Member 10834062 2-Jul-14 11:27am    
Hi,

I checked the IP and port - I get a connection. I tried the code above but still no change.
Member 10834062 2-Jul-14 11:27am    
Thanks though

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