This is a class from my code.
string to = message.Text;
string from = "noreply#zayzaycorporation.onmicrosoft.com";
MailMessage messagex = new MailMessage(from, to);
string mailbody = "Welcome!";
messagex.Subject = "Hello";
messagex.Body = mailbody;
messagex.Attachments.Add(new System.Net.Mail.Attachment("C:\\doc\\start.pdf"));
messagex.BodyEncoding = Encoding.UTF8;
messagex.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("noreply#zayzaycorporation.onmicrosoft.com", "XXX");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
client.Send(messagex);
context.Done(string.Empty);
The class sends the email with an attachment, works good. But my problem is when it's published as a Web Service in azure because there doesn't find any folder named "doc" on "C:\\". How can it be referenced the attachment to a folder from the main solution?
Thanks.
The Attachment class can be constructed using a Stream, which can thus contain anything and come from anywhere.
So basically generate the attachment in memory and wrap it in a MemoryStream or similar.
Try this..
String path = Application.StartupPath + "\doc\test.xml";
Related
string html = "<img src=data:image/png;base64,i0KGgAD34DtaA..........*>"; //*very long
SensMail(mailaddress,html, System.Text.Encoding.UTF8);
public static void SensMail(string sendTo, string body, System.Text.Encoding enCode)
{
string emailaddress = "mail#gmail.com";
string password = "it's secret";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress(sendTo);
mail.To.Add(sendTo);
mail.BodyEncoding = enCode;
mail.IsBodyHtml = true;
mail.Subject = "subject";
mail.Body = body;
SmtpServer.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential(emailaddress,password);
SmtpServer.Credentials = NetworkCred;
SmtpServer.EnableSsl = true;
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.Send(mail);
}
Why is the image not displayed ??
I tried to send to two different email providers, without success.
Is this happening for security reasons?
How can this problem be solved?
In HTML it does work and you see the image.
Outlook doesn't understand base64 images. There are two possible ways:
Upload the image file to any web server and then use the image URL in the HTML body for that image. Be aware, Outlook may block external links (even for images) automatically.
You may add an image file as an attachment and then use the cid prefix for the img tag. See Send inline image in email for more information.
I have this codes:
epost.From = new MailAddress("test.31#gmail.com");
epost.To.Add(textBoxMail.Text.ToString());
epost.Subject = textBoxSubject.Text.ToString();
epost.Body = "c:\\Users\\Raşit\\AppData\\Local\\MyText.txt"; //But i have a problem :(
SmtpClient smpt = new SmtpClient();
smpt.Credentials = new System.Net.NetworkCredential("test.31#gmail.com", "mypassword");
smpt.Host = "smtp.gmail.com";
smpt.EnableSsl = true;
smpt.Port = 587;
smpt.Send(epost);
MessageBox.Show("Done!");
Then I Started Debugging and I saw this:
I want file only, I don't want path.
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:\\Users\\Raşit\\AppData\\Local\\MyText.txt");
epost.Attachments.Add(attachment);
I want to send a mail in xamarin.android.I can send as text with the code below without image but the problem occurs when i try to send with an image as logo. I mean, i can't find the path of the image.
My sending mail method is:
public static void SendMail(List<string> to, List<string> cc, string subject, string body,string mfrom)
{
string messageHeader = "Android E-Mail Testi";
MailMessage msg = new MailMessage();
msg.From = new MailAddress(mfrom);
msg.To.Add(new MailAddress(to[0]));
msg.CC.Add(new MailAddress(cc[0]));
var inlineLogo = new LinkedResource("Drawable://logo.png");//This path is not working.
inlineLogo.ContentId = Guid.NewGuid().ToString();
msg.Body = string.Format(#"<img class=""img-responsive"" src=""cid:{0}"" style=""width:25%; float:left""/>
<br/><br/><h3>" + messageHeader + #"</h3>" + body, inlineLogo.ContentId);
var view = AlternateView.CreateAlternateViewFromString(msg.Body, null, "text/html");
view.LinkedResources.Add(inlineLogo);
msg.AlternateViews.Add(view);
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.SubjectEncoding = Encoding.UTF8;
SmtpClient smtpClient = new SmtpClient("xx.xxx.x.xxx");
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
List<MemoryStream> mStreams = null;
msg.BodyEncoding = Encoding.Unicode;
smtpClient.Send(msg);
if (mStreams != null)
foreach (MemoryStream mStream in mStreams)
mStream.Close();
}
I can send mail by using this code in asp.net.Only difference is file path. I used it like that and it works for web development:
var inlineLogo = new LinkedResource(HostingEnvironment.MapPath("~/images/logo.png"));
Which method should i use for image(logo.png) path in xamarin?
var inlineLogo = new LinkedResource(???);
I don't know the real path.How can i reach the path of the image in the drawable folder of the Resources folder?
Directory is:
Maybe it will help you. I created ToolbarItem with icon from the same folder Resources/drawable in such way:
new ToolbarItem("ToolbarItemName", "iconName.png", () => { //Some action; })
I have an .Net 4.5 application that sends an email, with an attachment. It works as expected when the email is opened on a desktop, but when opened on a mobile (iPhone in this case) the attachment shows as inline HTML not as an attachment.
When however I forward the same email from my desktop to the phone, the attachment shows up correctly on my phone so I am almost certain that it has to do with how I am specifying mime or content-type, disposition etc. but I can't see what I am doing wrong.
Here is the code - note that
att.ContentType = new System.Net.Mime.ContentType("multipart/mixed");
does create an attachment on iPhone but it is of type = mime-attachment that will not open.
I'm stumped & client awaits - any help greatly appreciated !
private void SendNotice(string body, string attachment, string email, bool pdf = false)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(ConfigurationManager.AppSettings["SMTP.SendFrom"]);
message.Subject = ConfigurationManager.AppSettings["MatchedNoticeSubject"];
message.To.Add(new MailAddress(email));
message.ReplyToList.Add(new MailAddress(ConfigurationManager.AppSettings["SMTP.ReplyTo"]));
message.Body = body;
message.IsBodyHtml = true;
Attachment att = Attachment.CreateAttachmentFromString(attachment, "SeniorInfo.html", System.Text.Encoding.ASCII, "text/html");
//specifying this creates an attachment of type "mime-attachment" that does not open
//att.ContentType = new System.Net.Mime.ContentType("multipart/mixed");
message.Attachments.Add(att);
SmtpClient server = new SmtpClient()
{
EnableSsl = (ConfigurationManager.AppSettings["SMTP.EnableSSL"].ToLower() == "true"),
Host = ConfigurationManager.AppSettings["SMTP.Server"],
Port = Convert.ToInt16(ConfigurationManager.AppSettings["SMTP.Port"]),
Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTP.Account"], ConfigurationManager.AppSettings["SMTP.Password"])
};
server.Send(message);
}
Solved after some trial and error fiddling.
Counter-intuitively the attachment ContentDisposition object is READONLY which lead me to believe that I couldn't meddle in it however the read object is apparently a reference to the actual Attachment.ContentDisposition since setting values on the read instance does (apparently) correct the problem. Also used the Enum for MediaTypeNames (System.Net.Mime.MediaTypeNames.Text.Html) tho I don't think that was the issue.
Email send now looks like this :
private void SendMatchNotice(string body, string attachment, string email, bool pdf = false)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(ConfigurationManager.AppSettings["SMTP.SendFrom"]);
message.Subject = ConfigurationManager.AppSettings["MatchedNoticeSubject"];
message.To.Add(new MailAddress(email));
message.ReplyToList.Add(new MailAddress(ConfigurationManager.AppSettings["SMTP.ReplyTo"]));
message.Body = body;
message.IsBodyHtml = true;
// Create the file attachment for this e-mail message.
Attachment att = Attachment.CreateAttachmentFromString(attachment, "SeniorInfo.html", System.Text.Encoding.ASCII, System.Net.Mime.MediaTypeNames.Text.Html);
System.Net.Mime.ContentDisposition disposition = att.ContentDisposition;
disposition.DispositionType = "attachment";
disposition.Inline = false;
disposition.FileName = "SeniorInfo.html";
disposition.CreationDate = DateTime.Now;
disposition.ModificationDate = DateTime.Now;
disposition.ReadDate = DateTime.Now;
message.Attachments.Add(att);
SmtpClient server = new SmtpClient()
{
EnableSsl = (ConfigurationManager.AppSettings["SMTP.EnableSSL"].ToLower() == "true"),
Host = ConfigurationManager.AppSettings["SMTP.Server"],
Port = Convert.ToInt16(ConfigurationManager.AppSettings["SMTP.Port"]),
Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTP.Account"], ConfigurationManager.AppSettings["SMTP.Password"])
};
server.Send(message);
}
Using the simple SMTP C# code below to send an email, how can i send an email template?
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = "http://www.yoursite.com/email.htm";
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Currently, as expected the received email just contains the URL for the template. how can i get it to send the template?
System.Net.WebClient client = new System.Net.WebClient();
string html = client.DownloadString("http://www.yoursite.com/email.htm");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = html;
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Your question is actually about reading a string from url and one of the possible answers is:
var url = "http://www.yoursite.com/email.htm";
var body = "";
using(var client = new WebClient()) {
body = client.DownloadString(url);
}
If the file is local, instead of using a download, you can simply read it in using System.IO, such as
string html;
System.IO.StreamReader fstream;
fstream = File.OpenText("yourpathgoeshere.html");
html = fstream.ReadToEnd();
fstream.Close();
after this, just assign the rest of the properties as suggested in the other posts. If the html file you're after is stored locally, this is probably better, or if it will be accessed frequently, it may be a better idea to store it locally and use this method.
note, you will need to import System.IO for this to work correctly.