|
-
Dec 13th, 2007, 04:54 AM
#1
Thread Starter
New Member
[2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??
am using .NET 2.0 Visual Studio 2005 using C#.
I'm attaching files using System.Net.Mail.Attachment.Add(...)
My problem is, that this is working fine, when I'm debugging with visual studio.
but in Livemode the file-attachments seems not to work. (no error is thrown but I don't get the mail)
Calling the site via localhost also sends no mail.
Sending Mails using this System.Net.Mail-Class always works fine (without attachments).
Please help!!
here is the code
c# Code:
xxxxxxxxxxxxxxxxxxxxxxxx
public void sendmail(string frm, string to, string bcc, string cc, string sub, string bdy, string smtpserver, string smtpusername, string smtppassword)
{
int fileLen=0;
string pathAndFile = "";
try
{
if (smtpusername != "" && smtppassword != "")
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(frm, to, sub, bdy);
if (bcc != "")
msg.Bcc.Add(bcc);
if (cc != "")
msg.CC.Add(cc);
if (txtFileUpload.HasFile)
{
pathAndFile = txtFileUpload.PostedFile.FileName;
fileLen = (txtFileUpload.PostedFile.ContentLength / 1000);
if (fileLen > 500)
{
CreateMessageAlert(this, "Maximum File Size Limit is 500 kbps", "alert", "MyMessages.aspx?link=3");
return;
}
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(pathAndFile);
msg.Attachments.Add(attachment);
}
msg.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(smtpserver);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(smtpusername, smtppassword);
smtpClient.Send(msg);
if (pathAndFile != "")
{
msg.Attachments.Dispose();
}
}
else
{
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
msg.From = frm;
msg.To = to;
msg.Bcc = bcc;
msg.Cc = cc;
msg.Subject = sub;
if (pathAndFile!="")
{
msg.Attachments.Add(new MailAttachment(pathAndFile));
}
msg.BodyFormat = MailFormat.Html;
msg.Body = bdy;
SmtpMail.SmtpServer = smtpserver;
SmtpMail.Send(msg);
if (pathAndFile != "")
{
msg.Attachments.Clear();
// msg.Attachments.Dispose();
}
}
}
catch
{}
}
sendmail(Session["userid"].ToString(), email, tempbcc, tempcc, txtSubject.Text, body, smtpserver, smtpusername, smtppassword);
Last edited by Hack; Dec 13th, 2007 at 07:57 AM.
Reason: Added Highlight Tags
-
Dec 15th, 2007, 12:28 PM
#2
Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??
Hmm, you say no email arrives but there isn't an "error". I beg to differ. You never, ever use empty catch blocks as you have in lines 121 through 123 of your sample.
Out of interest - kbps is "kilobits per second"; it doesn't make sense to use this as a file size limit.
-
Dec 15th, 2007, 05:35 PM
#3
Hyperactive Member
Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??
i agree with axion, your code should not have run in the first place, your catch should contain somthing like:
catch (Exception ex) { <imo this can be left empty but for user-friendliness always try and inform the user that there is an error...> };
-
Dec 16th, 2007, 01:45 AM
#4
Re: [2.0] System.Net.Mail.Attachment.Add is only working in Debug Mode??
If you remove the try-catch block. what exception do you get?
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|