Sending Email With Attachment C# [duplicate] - c#

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Send email with attachment from WinForms app?
Here is my script:
var fromAddress = new MailAddress("myemail#gmail.com");
var toAddress = new MailAddress("myemail#gmail.com");
const string fromPassword = "mypassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
smtp.Send(message);
}
It works well, yet I am yet to come up with a way to add an attachment. Yes, I know this site has examples, but I cannot find one that will send an attachment

Use the Attachments property.

Related

I have an error when sending mail from the programming language C#

I am sending emails from a C# method, where from one moment to another it stops working and I allow access to my host.
I have not uploaded changes to production, for a long time and I have even less touched this part of the code. I get the following error but I don't know what it means and what solution to give about it:
Error: IoException:
Handshake failed due to unexpected packet format
Code:
string mailFrom = emailSettings.Correo;
string nameFrom = emailSettings.Nombre;
string passwordFrom = emailSettings.Password;
string hostSMTP = emailSettings.HostSMTP;
// Message data
MailAddress fromAddress = new MailAddress(mailFrom, nameFrom, Encoding.UTF8);
MailAddress toAddress = new MailAddress(mailDestino, nombreDestinatario, Encoding.UTF8);
// Specify the message content.
using (MailMessage message = new MailMessage(fromAddress, toAddress)
{
Subject = asuntoMensaje,
SubjectEncoding = Encoding.UTF8,
IsBodyHtml = true,
Body = cuerpoMensaje,
BodyEncoding = Encoding.UTF8,
Priority = MailPriority.Normal,
})
{
// SMTP Cliente
SmtpClient smtp = new SmtpClient
{
Host = hostSMTP,
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(mailFrom, passwordFrom),
DeliveryMethod = SmtpDeliveryMethod.Network,
Timeout = 2 * 60 * 1000 //2 minutos
};
message.Headers.Add("Disposition-Notification-To", mailFrom);
message.Headers.Add("Return-Receipt-To", mailFrom);
// Send the E-Mail
smtp.Send(message);
}
Thank you very much, I look forward to your response.

converting to system.net.mail.mailMessage error

i'm actually trying to create registration page with verfication mail using MVC in visual studio, but here to send a message im getting
error : 'RandLform.Controllers.MailMessage' to 'System.Net.Mail.MailMessage' RandLform
public void SendVerficationLinkEmail(string emailID, string activationCode)
{
var VerifyUrl = "/User/VerifyAccount/" + activationCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, VerifyUrl);
var fromEmail = new MailAddress("lokeshkingdom4u#gmail.com", "Lokesh Pladugula");
var toEmail = new MailAddress(emailID);
var fromEmailPassword = "paisa007";
string subject = "Account created Succesfully!";
string body = "<br/>To verify your account, click on below link.<br/><br/> "+" "+link+"";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NewNetworkCredential(fromEmail.Address, fromEmailPassword)
};
using (var message = new MailMessage(fromEmail, toEmail)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
smtp.Send(message);
}
Lokesh Paladugula,
I recommend you to change password of your email account.
I'm not sure if this is actual error, please send proper error.

Gmail sending email timing out

I am trying to send an email by gmail but this code isn't working, gives connection time out error. If I make the port '587' it gives this error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
string email = // email
string password = // password
string smtp = // smtp.gmail.com
int port = // 465
var from = new MailAddress(email, "");
var to = new MailAddress(message.Destination);
var client = new SmtpClient()
{
Host = smtp,
Port = port,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(email, password)
};
var mail = new MailMessage(from, to)
{
Subject = subject,
Body = body,
IsBodyHtml = true
};
return client.SendMail(mail);
}
I have used very similar code to yours in the past and it worked, but the port number I used was 587.
This is my code:
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("gmail_login", "gmail_password")
};
using( var message = new MailMessage("your_emailAddress", "destination_Email")
{
Subject ="subject",
Body = "body"
})
client.Send(message);
If you are logging from a new destination/timezone you should first log in through the web browser and confirm that it is you :)
I recently posted another answer that addresses this situation:
You need to make sure that the email account that you're using allows
access from less secure apps.
Simply change a security setting from your account.
Try it here

C# Send Email with <b> and <u> [duplicate]

This question already has answers here:
MailMessage c# - How to make it HTML and add images etc?
(5 answers)
Closed 8 years ago.
Hi I am trying to send an email using Bold and Underline in my Message in C#
<b></b> <u></u>
How would I implement this into my code??
var fromAddress = new MailAddress("test#gmail.com", "test");
var toAddress = new MailAddress("test#gmail.com", "test");
const string fromPassword = "REMOVED";
const string subject = "Engineering Completed NewParts Project";
const string body = "Engineering has completed their data entry on the <b><u>NewParts</b></u> project on PDMTool. / ";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Thank you.
Fix your new MailMessage declaration like so:
new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true // this tells the message to be sent in HTML
}
Set the IsBodyHtml property to true:
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
}

Getting smtpException when attemping to send mail C#

I'm trying to send an email through SmtpClient using this code
var client = new SmtpClient("smtp.gmail.com", 465)
{
Credentials = new NetworkCredential("***#gmail.com", "password"),
EnableSsl = true,
};
client.Send("***#gmail.com", "***#gmail.com", "test", "testbody");
What I'm getting is smtpException "Message could not be sent".
System.Net.Mail.SmtpException: Message could not be sent. ---> System.Exception:Connectionclosed at System.Net.Mail.SmtpClient.Read () [0x000f9] in /private/tmp/source/bockbuild-mono-
I'm using Mono on Mac (OSX 10.9.2)
My credentials and host/port are correct.
Maybe I need to enable it through my gmail account somehow?
Thanks!
Use:
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}

Categories

Resources