Why do I get 'property cannot be assigned when sending an SMTP email?
mail.To
and mail.From
are readonly. Move them to the constructor.
using System.Net.Mail;
...
MailMessage mail = new MailMessage("you@yourcompany.example", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
Property or indexer Attachments cannot be assigned to -- it is read only
Why complicate the code like that. You need to use message.Attachments.Add
since the Attachments
property is read-only :
var message = new MailMessage(fromAddress, toAddress)
{
Subject = Environment.MachineName,
Body = "PC NAME : " + Environment.MachineName + "\r\nIP ADRESS : " + Dns.GetHostEntry(Dns.GetHostName()).AddressList[1],
};
message.Attachments.Add(new Attachment(@"c:\MyDir\Img" + "/img" + (Saving.CountImagesTaken(@"c:\MyDir\Img") - 1).ToString() + ".png"));
using (message)
{
smtp.Send(message);
}
Send mail to user - SmtpException causes problems
I have designed a component that handles Emails
First add my class to your project.
using System.ComponentModel;
using System.Net.Mail;
using System.Net;
using System.ComponentModel.DataAnnotations;
namespace Hector.Framework.Controls
{
public class MailMessageControl : Component
{
private MailMessage Mail = new MailMessage();
private SmtpClient SmtpClient = new SmtpClient();
public MailMessageControl()
{
Host = "smtp.gmail.com";
Port = 587;
EnableSSL = true;
}
public string Host
{
get => SmtpClient.Host;
set => SmtpClient.Host = value;
}
public int Port
{
get => SmtpClient.Port;
set => SmtpClient.Port = value;
}
public bool EnableSSL
{
get => SmtpClient.EnableSsl;
set => SmtpClient.EnableSsl = value;
}
public void AttachFile(string path)
{
Mail.Attachments.Add(new Attachment(path));
}
public void SetCredentials(string mail, string password)
{
SmtpClient.Credentials = new NetworkCredential(mail, password);
}
public void SetSender(string mail)
{
Mail.From = new MailAddress(mail);
}
public void AddAddressSee(string mail)
{
Mail.To.Add(mail);
}
public void SetSubject(string subject)
{
Mail.Subject = subject;
}
public void SetBody(string body, bool isHTML)
{
Mail.IsBodyHtml = isHTML;
Mail.Body = body;
}
public bool SendEmail()
{
try
{
SmtpClient.Send(Mail);
return true;
}
catch
{
return false;
}
}
public bool IsValidEmail(string email)
{
try
{
return new MailAddress(email).Address == email;
}
catch
{
return false;
}
}
public bool EmailIsValidated(string email)
{
return new EmailAddressAttribute().IsValid(email);
}
}
}
Usage example:
Hector.Framework.Controls.MailMessageControl mail = new Hector.Framework.Controls.MailMessageControl();
mail.SetCredentials("Your gmail email", "Your gmail password");
mail.SetSender("Sender mail");
mail.AttachFile("Your file path"); //If you want send file
mail.AddAddressSee("Add mail to receive your message");
mail.SetSubject("Subject");
mail.SetBody("Body", false);
if(mail.SendEmail())
{
//Mail send correctly
}
else
{
//Error
}
Now go to the following link: https://myaccount.google.com/lesssecureapps
And enable this switch:
This allows your program to use your credentials to send emails, if you do not activate it, it may result in an error.
After activating it, try to send a message
Sending E-mail for SMTP error
MailMessage.To
is a read-only property. It returns a list of MailAdresses you added to the message.
To add a MailAddress, you should use:
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
System.ObjectDisposedException while sending email using smtpclient (fluent-email)
The extensions provided by fluent-email did inject these classes as singleton and I did use another lifetime and therefore this issue occured.
So I forgot to also override the dependency injection container configuration for all dependencies using the System.ObjectDisposedException
to use singleton, since I was using a custom implementation of FluentEmail.Smtp.SendMailEx.SendMailExImplAsync
.
After overridding the other dependencies in the DI extension, it worked.
Related Topics
Get List of Connected Usb Devices
How to Convert a Structure to a Byte Array in C#
Covariance and Contravariance Real World Example
Integer Summing Blues, Short += Short Problem
How to Generate a Stream from a String
Detect If Deserialized Object Is Missing a Field With the Jsonconvert Class in Json.Net
How to Secure an ASP.NET Web API
How to Replace the *First Instance* of a String in .Net
.Net Httpclient. How to Post String Value
A Generic Error Occurred in Gdi+, Jpeg Image to Memorystream
Enum Tostring With User Friendly Strings
Ipc Mechanisms in C# - Usage and Best Practices
Validating an Xml Against Referenced Xsd in C#
.Net Process.Start Default Directory
Which .Net Dependency Injection Frameworks Are Worth Looking Into