I have a [WebMethod] Sendemail This works fine but now i what to upgrade it to send attachments. I am using a fileupload. This is my method Call.
lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);
My Call Statement is underlined in blue and the two error given look like:
*1)*The best overloaded method match for 'WebTestServiceApp.localhost.Service1.Sendemail(string, string,
string, string, WebTestServiceApp.localhost.Stream)' has some invalid
arguments
*2)*Argument 5: cannot convert from 'System.IO.Stream' to 'WebTestServiceApp.localhost.Stream'
FileUpload1.PostedFile.FileName is passed as a String FileUpload1.FileContent is passed as a stream
This is my [WebMethod], Now u all can see every thing i can see, i can't spot anything wrong, But i am unsure if FileUpload1.FileContent should be passed as a Stream.
[WebMethod]
public string Sendemail(String inValueTo, String inValueSub, String inValueBody, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent) //, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent
{
try
{
String valueTo = inValueTo;
String valueSub = inValueSub;
String valueBody = inValueBody;
String valueAttachmentPostedfile = inValueAttachmentPostedfile; //FileUpload1.PostedFile.FileName
Stream valueAttachmentFileContent = inValueAttachemtnFileContent; //FileUpload1.FileContent.fileName
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); // Creating new message.
message.To.Add(valueTo);
message.Subject = valueSub;
message.From = new System.Net.Mail.MailAddress("shaunmossop#mweb.co.za");
message.Body = valueBody;
message.IsBodyHtml = true;
string fileName = Path.GetFileName(valueAttachmentPostedfile); // Get attachment file
Attachment myAttachment =
new Attachment(valueAttachmentFileContent, fileName);
if (fileName != "")
{
message.Attachments.Add(myAttachment); // Send attachment
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com"); //Properties.Settings.Default.MailSMTPServer
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential netC = new NetworkCredential(Properties.Settings.Default.username, Properties.Settings.Default.password); // Useing Projects defult settings.
smtp.Credentials = netC;
smtp.Send(message);
return "Message has been sent";
}
catch (Exception)
{
return "Message faild to send" ;
}
}
the second error it gives you hits the nail on the head, yes youre passing string string string string but youre passing the wrong type of stream for it to interact with, it wants this kind WebTestServiceApp.localhost.Stream
youre giving it this kind
System.IO.Stream
when you use the stream try explicitly declaring it as a WebTestServiceApp.localhost.Stream when you first make it, that way youre then passing the right types of stream and your problem should stop!
theres two types of stream you see, one used for web apps and one used for desktop apps.
Related
Alright so pretty much I know there is a simple solution for this for the life of me though I can't find it. I want to send an attachment via mail, now I have it so that it thinks it's going to send an attachment like:
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
message.Attachments.Add(new Attachment(add_photo.FileName));
client.Send(message);
You know but if add_photo(The File Dialog) is emtpy it throws and error, I tried adding a catch statement for it but the program just kinda crashes almost (not like crashes crashes but functionality wise).
Anyway, I was thinking if there is no file selected by the dialog I'll just set one myself, something really small that wouldn't even matter. So I have a picture in my resources called 'DD.png' and I would like to set it if there is no file in the dialog any ideas?
Here's what I have:
if (!string.IsNullOrEmpty(add_photo.FileName))
{
add_photo.FileName = (Path.GetFullPath(Turbo_Bomber.Properties.Resources.DD.ToString()));
}
#region Providers
if (provider == "AT&T")
{
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
message.Attachments.Add(new Attachment(add_photo.FileName));
client.Send(message);
} // etc
Any ideas? Thank you guys.
Stick with your first go, with a small change:
message.To.Add(recieve + "#txt.att.net");
message.From = new MailAddress(user);
message.Subject = subject;
message.Body = body;
if (!string.IsNullOrEmpty(add_photo.FileName))
{
message.Attachments.Add(new Attachment(add_photo.FileName));
}
client.Send(message);
Now you don't need to add a 'mystery' attachment.
I'm developing an windows application in which i need to send some files as attachment through email.
Code
public string SendMail(string mFrom,
string mPass,
string mTo,
string mSub,
string mMsg,
string mFile,
bool isDel)
{
string sql = "";
try
{
System.Net.Mail.MailAddress mailfrom = new System.Net.Mail.MailAddress(mFrom);
System.Net.Mail.MailAddress mailto = new System.Net.Mail.MailAddress(mTo);
System.Net.Mail.MailMessage newmsg = new System.Net.Mail.MailMessage(mailfrom, mailto);
newmsg.IsBodyHtml = false;
if (mFile.Length > 2
&& File.Exists(mFile))
{
System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(mFile);
newmsg.Attachments.Add(att);
}
newmsg.Subject = mSub;
newmsg.Body = mMsg;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(mFrom, mPass);
smtp.EnableSsl = true;
smtp.Send(newmsg);
newmsg.Dispose();
GC.Collect();
sql = "OK";
if (isDel
&& File.Exists(mFile))
{
File.Delete(mFile);
}
}
catch (Exception ex)
{
sql = ex.Message;
}
return sql;
}
This code works fine for small files.But i need to send large files up to 1-2 GB.
For that what to do.
You cannot use e-mail to get these files across and this has nothing to do with your code.
I don't think there is ANY provider out there who will support sending files of that size let alone receiving them. Even G-Mail has a limit of 25 Mb which is quite large already.
E-Mail is not the proper channel to do this.
So the problem will not be in your code, the provider will limit the size of the attachment and just refuse them when you present them with a larger file. You will get an e-mail back at your FROM address stating that the file is too large and your e-mail did not get across.
For doing this in the simplest form probably look at FTP.
I do agree with Gerald Versluis in that email is not the proper channel for this. Even if you are using your own email server that is configurable there is probably some internal limit that prevents it from sending such big files.
I’d go with FTP for this but if you really want to continue with email I’d suggest you check following first.
Is there connection timeout property on the server? If yes then try to increase it to 3 hours or something like that.
Is there enough space on the mail server?
Is there some documentation for your email server? Are there any additional details regarding attachment size ?
I am using MailMessage class in my program. When the subject is too long subject will look like.
Subject:
=?utf-8?B?W0VudGVycHJpc2UgUHJpb3JpdHldIC0gQ3VzdG9tZXIgSW5jaWRlbnQgNjkxNzIgZm9yIEhhcmlkaGFyYW4gKDEzMjM5OSkgaGFyaWRoYXJhbnJAc3luY2Z1c2lvbi5jb20gOiBUZXN0aW5nIFRlc3RpbmcgVGVzdGluZyBUZXNpbmcgVGVzdGluZyBUZXN0aW5nIFRlc3RpbmcgVGVzdGluZyBUZXN0aW5nIFRlc3Rpbmcg4o"
This problem occurred in server only. while debugging i have used the same subject content in my "local" but, i got correct subject.
Program:
protected MailMessage msg;
msg.Subject = subject;
Got the same (error) subject in WebMail.IHostExchange.NET also.
What is the problem?
Update:
This is a part of my coding.
public EmailSenderThread(string emailAddresses, string ccemailaddress, string from, string subject, string body)
: base()
{
msgThread = new Thread(new ThreadStart(MailSender));
this.mailAddress = emailAddresses;
this.ccmailAddress = ccemailaddress;
msg.From = new MailAddress(from);
msg.IsBodyHtml = true;
msg.Body = body;
string[] mails = emailAddresses.Split(';');
foreach (string mail in mails)
if (!string.IsNullOrEmpty(mail))
msg.To.Add(mail);
if (ccemailaddress != string.Empty)
{
string[] ccemails = ccemailaddress.Split(';');
foreach (string ccmail in ccemails)
if (!string.IsNullOrEmpty(ccmail))
msg.CC.Add(ccmail);
}
msg.Subject = subject;
msgThread.Start();
}
I have already tried with
msg.SubjectEncoding = System.Text.Encoding.UTF8;
but i got the same error. Did you got my doubt. Please let me know if I didn't explain clearly.
1) why it is working fine in local? and why it is not working when i am hosting this into server. ?
2) What is the maximum length of the subject line?
2) What is the maximum length of the subject line?
From RFC822 about unstructured header fields:
Some field bodies in this standard are defined simply as "unstructured" (which is specified below as any US-ASCII characters, except for CR and LF) with no further restrictions. These are referred to as unstructured field bodies. Semantically, unstructured field bodies are simply to be treated as a single line of characters with no further processing (except for header "folding" and "unfolding" as described in section 2.2.3).
The subject line is an unstructured field, and therefore has no imposed length limit.
Without seeing more code, I'm going to guess an encoding problem - try specifying an encoding for your subject and body. Look at this post for sample code.
I was wondering if it is possible through the .NET 2.0 MailMessage object to send an inline MHTML file that is created on the fly.
By inline I mean: It should be sent in a way that the user can see it, once he opens the email, without having to open/download the attachment.
It's a bit tricky, but yes, you can do it. In fact MailMessage class is nothing more than a wrapper above the system's CDO.Message class which can do the trick.
Also you can use AlternateView functionality, it's more simple:
MailMessage mailMessage = new MailMessage("me#me.com"
,"me#me.com"
,"test"
,"");
string ContentId = "wecandoit.jpg";
mailMessage.Body = "<img src=\"cid:" + ContentId + "\"/>";
AlternateView av = AlternateView.CreateAlternateViewFromString(mailMessage.Body
,null
,MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(#"d:\Personal\My Pictures\wecandoit.jpg");
lr.ContentId = ContentId;
lr.ContentType.Name = ContentId;
lr.ContentType.MediaType = "image/jpeg";
av.LinkedResources.Add(lr);
mailMessage.AlternateViews.Add(av);
SmtpClient cl = new SmtpClient();
cl.PickupDirectoryLocation = #"c:\test";
cl.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
cl.Send(mailMessage);
(jdecuyper -- thanks for the plug, as I wrote aspNetEmail).
You can do this with aspNetEmail. You can replace the entire contents of the email message with your MHT.
You can't do this with System.Net.Mail, but if you want to go the commerical route, pop me an email at dave#advancedintellect.com and I'll show you how this can be done.
If you want to go an open source route, there is probably some SMTP code on codeproject that you could modify to do this. Basically, you would inject your contents into the DATA command of the SMTP process.
One thing to note: If your MHT document has embedded scripts, flash, activeX objects or anything that may be blocked by the mail client, it probably won't render the same as what you are seeing in the browser.
Are you trying to add some images to an html email?
To accomplish this you will need to embed the images inside your email. I found a tutorial to accomplish it in a few lines of code. You can also buy the aspnetemail assembly. It has always helped me a lot to send emails with embedded images, they also have an excellent support team if anything goes wrong.
Keep in mind that embedding images makes your email heavier, but nicer :)
It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):
protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
string smtpAddress = "smtp.email.com";
try
{
CDO.Message oMessage = new CDO.Message();
// set message
ADODB.Stream oStream = new ADODB.Stream();
oStream.Charset = "ascii";
oStream.Open();
oStream.WriteText(MHTmessage);
oMessage.DataSource.OpenObject(oStream, "_Stream");
// set configuration
ADODB.Fields oFields = oMessage.Configuration.Fields;
oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
oFields.Update();
// set other values
oMessage.MimeFormatted = true;
oMessage.Subject = subject;
oMessage.Sender = emailFrom;
oMessage.To = emailTo;
oMessage.Send();
}
catch (Exception ex)
{
// something wrong
}
}
It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):
protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
string smtpAddress = "smtp.email.com";
try
{
CDO.Message oMessage = new CDO.Message();
// set message
ADODB.Stream oStream = new ADODB.Stream();
oStream.Charset = "ascii";
oStream.Open();
oStream.WriteText(MHTmessage);
oMessage.DataSource.OpenObject(oStream, "_Stream");
// set configuration
ADODB.Fields oFields = oMessage.Configuration.Fields;
oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
oFields.Update();
// set other values
oMessage.MimeFormatted = true;
oMessage.Subject = subject;
oMessage.Sender = emailFrom;
oMessage.To = emailTo;
oMessage.Send();
}
catch (Exception ex)
{
// something wrong
}
}
I have a script that sends an e-mail as both plain text and HTML, and it works fine for most e-mail readers including Outlook and Gmail. However, when reading the message on a Windows Mobile smartphone, the output is:
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDMuMiBGaW5hbC8vRU4i Pg0KPEhUTUw+DQo8SEVBRD4NCiAgICA8TUVUQSBIVFRQLUVRVUlWPSJDb250ZW50LVR5cGUi IENPTlRFTlQ9InRleHQvaHRtbDtjaGFyc2V0PWlzby04ODU5LTEiPg0KICAgIDxUSVRMRT5Z b3VyIE1lZ2Fwb255IFBhc3N3b3JkIC0gTWVnYXBvbnkgLSBEaXNjb3ZlciB0aGUgbmV4dCBi aWcgdGhp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ICAgICA8Rk9OVCBTSVpFPSI0IiBGQUNFPSJBcmlh bCIgQ09MT1I9IiM5OTAwMDAiPk1lZ2Fwb255LmNvbTwvRk9OVD48L0E+DQogICAgICAgIDxG T05UIFNJWkU9IjQiIEZBQ0U9IkFyaWFsIj4NCiAgICAgICAgICAgIHRvIGFjY2VzcyB5b3Vy IGFjY291bnQuDQogICAgICAgICAgICA8QlI+DQogICAgICAgICAgICA8QlI+DQogICAgICAg ICAgICBUaGFuayB5b3UgZm9yIHlvdXIgc3VwcG9ydCBvZiBNZWdhcG9ueSBhbmQgaW5kZXBl bmRlbnQgbXVzaWMhPEJSPg0KICAgICAgICAgICAgPEJSPg0KICAgICAgICAgICAgU2luY2Vy ZWx5LDxCUj4NCiAgICAgICAgICAgIDxCUj4NCiAgICAgICAgICAgIFRoZSBNZWdhcG9ueSBU ZWFtPEJSPg0KICAgICAgICAgICAgPEJSPg0KICAgICAgICAgICAgPEJSPg0KICAgICAgICA8 L0ZPTlQ+PEZPTlQgU0laRT0iMiIgRkFDRT0iQXJpYWwiPipUaGlzIGlzIGFuIGF1dG9tYXRl ZCBtZXNzYWdlLiBQbGVhc2UgZG8gbm90IHJlcGx5LjwvRk9OVD4NCjwvQk9EWT4NCjwvSFRN TD4NCg==
The correct output should be: Click here to see
The code is:
SmtpClient mC = new SmtpClient(ConfigurationManager.AppSettings["smtpServer"]);
NetworkCredential nC = new NetworkCredential(ConfigurationManager.AppSettings["smtpUsername"], ConfigurationManager.AppSettings["smtpPassword"]);
mC.UseDefaultCredentials = false;
mC.Credentials = nC;
MailAddress mFrom = new MailAddress("noreply#megapony.com", "Megapony");
MailAddress mTo = new MailAddress(forgotpwemail.Text);
MailMessage mMsg = new MailMessage(mFrom, mTo);
mMsg.IsBodyHtml = false;
mMsg.Subject = "Your Megapony Password";
mMsg.Body = getForgotPWBodyPlain(result);
System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(getForgotPWBody(result), mimeType);
mMsg.AlternateViews.Add(alternate);
try
{
mC.Send(mMsg);
pnlpwform.Visible = false;
pnlSuccess.Visible = true;
}
catch (Exception)
{
pnlResponse.Visible = true;
}
mMsg.Dispose();
Please help!
Thanks,
Paul
I would say it's a combination of the content-type and the the image that's causing the issue. The link you provided showed a nice gif with a Megapony logo, yet the content-type is set to text only. Because of this, the bits that make up the gif is being treated as a series of text characters.
This would be a good place to start: http://www.systemnetmail.com/faq/3.1.3.aspx