Click here to Skip to main content
15,999,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to send an email from one to another mail and also want to send an attachment when i use my code in client machine it works, but if i used after putting it in web it didn,t work.web link is www.akdpl.com/carrer.aspx.
I am giving my code also

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Text;
using System.Configuration;
using System.Data.OleDb;
//using System.Xml.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;



protected void Button1_Click1(object sender, EventArgs e)
{
try
{
string str_from_address = "contact@akdpl.com";
string str_name = "A K Dutta & Co. Pvt. Ltd.";
string str_to_address = txteid.Text;
MailMessage email_msg = new MailMessage();
email_msg.From = new MailAddress(str_from_address, str_name);
email_msg.Sender = new MailAddress(str_from_address, str_name);
email_msg.ReplyTo = new MailAddress(str_from_address, str_name);
email_msg.To.Add(str_to_address);
email_msg.Subject = "Request Confirmation"
email_msg.Body = "Thank you for showing your interest in A K Dutta & Co. Pvt. Ltd. .We will get back to you very soon"
email_msg.Priority = MailPriority.Normal;"
SmtpClient mail_client = new SmtpClient();
NetworkCredential network_cdr = new NetworkCredential();
network_cdr.UserName = str_from_address;
network_cdr.Password = "password";
mail_client.Host = "smtp.akdpl.com";
mail_client.UseDefaultCredentials = false;
mail_client.Credentials = network_cdr;

mail_client.Send(email_msg);
//Its Working upto Here a mail is going but after that it didn't work
sendmail();
txteid.Text =""
txtname.Text = ""
txtmessage.Text = ""
txtcon.Text = ""
txtorg.Text = ""

Response.Redirect("thanksdet.html");
}
catch (SmtpException df)
{
Response.Write(df.ToString());
}
}




void sendmail()
{
string str_from_address = "contact@akdpl.com";
string str_name = txtname.Text;
string str_to_address = "contact@akdpl.com";
MailMessage email_msg = new MailMessage();
email_msg.From = new MailAddress(str_from_address, str_name);
email_msg.Sender = new MailAddress(str_from_address, str_name);
email_msg.ReplyTo = new MailAddress(str_from_address, str_name);
email_msg.To.Add(str_to_address);
email_msg.Subject = "Request logged";
Attachment s = new Attachment(FileUpload1.PostedFile.FileName);
email_msg.Attachments.Add(s);

email_msg.Body = "Candidate Name is " + txtname.Text + " Email Id: <" + txteid.Text + ">" + " Date of Birth : " + txtdob.Text + " Address : " + txtcon.Text + " Phone Number: " + txtphone.Text + " Education Qualification : " + txtedu.Text + " Post Applied For : " + txtpost.Text + " Year Experienced : " + txtyear.Text;
email_msg.Priority = MailPriority.Normal;
SmtpClient mail_client = new SmtpClient();
NetworkCredential network_cdr = new NetworkCredential();
network_cdr.UserName = str_from_address;
network_cdr.Password = "password";
mail_client.Host = "smtp.akdpl.com";
mail_client.UseDefaultCredentials = false;
mail_client.Credentials = network_cdr;

mail_client.Send(email_msg);
}
}
}
Posted

1 solution

Most likely reason is that the mail settings you're using are from your ISP and only work when you're logged in to their server, which the client server is not. Either way, these settings should exist in the web.config so they are easy to configure and only stored in one place. Your business layer should have a method that takes the parameters ( the body, subject, mailto ( unless you want to specify that in the web config too ), etc.

What happens, does it crash or just fail silently ? If it crashes, the error message will tell you what is wrong.
 
Share this answer
 
Comments
singh7pankaj 28-Jun-11 0:52am    
It just giving runtime error

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