.NET Core - Attachment causing Failure Sending Mail
up vote
0
down vote
favorite
I have the following code, which works when I don't have the attachment.add line. (I've blanked out the addresses & password).
Please help, I guess I'm doing something wrong with the attachment I'm just not sure what!
The inner exception is
{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ReferenceReadStream'. at
System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
var stream = file.OpenReadStream();
await sendEmail(stream, name);
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return ""
}
public async Task sendEmail(Stream stream, String filename){
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
c# .net-core smtp
add a comment |
up vote
0
down vote
favorite
I have the following code, which works when I don't have the attachment.add line. (I've blanked out the addresses & password).
Please help, I guess I'm doing something wrong with the attachment I'm just not sure what!
The inner exception is
{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ReferenceReadStream'. at
System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
var stream = file.OpenReadStream();
await sendEmail(stream, name);
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return ""
}
public async Task sendEmail(Stream stream, String filename){
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
c# .net-core smtp
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following code, which works when I don't have the attachment.add line. (I've blanked out the addresses & password).
Please help, I guess I'm doing something wrong with the attachment I'm just not sure what!
The inner exception is
{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ReferenceReadStream'. at
System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
var stream = file.OpenReadStream();
await sendEmail(stream, name);
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return ""
}
public async Task sendEmail(Stream stream, String filename){
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
c# .net-core smtp
I have the following code, which works when I don't have the attachment.add line. (I've blanked out the addresses & password).
Please help, I guess I'm doing something wrong with the attachment I'm just not sure what!
The inner exception is
{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ReferenceReadStream'. at
System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at
System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
var stream = file.OpenReadStream();
await sendEmail(stream, name);
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return ""
}
public async Task sendEmail(Stream stream, String filename){
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
c# .net-core smtp
c# .net-core smtp
edited Nov 2 at 16:30
asked Nov 2 at 16:14
Alec.
3,46741749
3,46741749
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29
add a comment |
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I think the stream is no read to end:
Try:
var fileContent = stream.ReadToEnd();
var attachment = new Attachment(fileContent, filename);
var smtpClient = new SmtpClient
Perhaps You have to read the filcontent before calling SendEmail
add a comment |
up vote
0
down vote
I think the stream needs to be readable, add
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
await sendEmail(file, name); //send the file not the open read stream
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return "";
}
public async Task sendEmail(IFormFile file, String filename){
using(var stream = file.OpenReadStream()){ //You open your stream here
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
}
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think the stream is no read to end:
Try:
var fileContent = stream.ReadToEnd();
var attachment = new Attachment(fileContent, filename);
var smtpClient = new SmtpClient
Perhaps You have to read the filcontent before calling SendEmail
add a comment |
up vote
0
down vote
I think the stream is no read to end:
Try:
var fileContent = stream.ReadToEnd();
var attachment = new Attachment(fileContent, filename);
var smtpClient = new SmtpClient
Perhaps You have to read the filcontent before calling SendEmail
add a comment |
up vote
0
down vote
up vote
0
down vote
I think the stream is no read to end:
Try:
var fileContent = stream.ReadToEnd();
var attachment = new Attachment(fileContent, filename);
var smtpClient = new SmtpClient
Perhaps You have to read the filcontent before calling SendEmail
I think the stream is no read to end:
Try:
var fileContent = stream.ReadToEnd();
var attachment = new Attachment(fileContent, filename);
var smtpClient = new SmtpClient
Perhaps You have to read the filcontent before calling SendEmail
answered Nov 3 at 19:34
ivion
12015
12015
add a comment |
add a comment |
up vote
0
down vote
I think the stream needs to be readable, add
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
await sendEmail(file, name); //send the file not the open read stream
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return "";
}
public async Task sendEmail(IFormFile file, String filename){
using(var stream = file.OpenReadStream()){ //You open your stream here
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
}
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
add a comment |
up vote
0
down vote
I think the stream needs to be readable, add
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
await sendEmail(file, name); //send the file not the open read stream
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return "";
}
public async Task sendEmail(IFormFile file, String filename){
using(var stream = file.OpenReadStream()){ //You open your stream here
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
}
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I think the stream needs to be readable, add
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
await sendEmail(file, name); //send the file not the open read stream
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return "";
}
public async Task sendEmail(IFormFile file, String filename){
using(var stream = file.OpenReadStream()){ //You open your stream here
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
}
I think the stream needs to be readable, add
[HttpPost]
public async Task<String> PostProfilePicture(IFormFile file, int ID)
{
var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
try
{
await sendEmail(file, name); //send the file not the open read stream
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return "";
}
public async Task sendEmail(IFormFile file, String filename){
using(var stream = file.OpenReadStream()){ //You open your stream here
var attachment = new Attachment(stream, filename);
var smtpClient = new SmtpClient
{
Host = "smtp.gmail.com", // set your SMTP server name here
Port = 587, // Port
EnableSsl = true,
Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
};
var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
message.Subject = "Hello Alec!!";
message.Body = "How are you doing.";
message.Attachments.Add(attachment);
await smtpClient.SendMailAsync(message);
}
}
}
edited 30 mins ago
answered Nov 2 at 16:21
Max
1263
1263
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
add a comment |
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
My file comes from IFormFile, not sure how to implement that as I don't have a physical copy of the file.
– Alec.
Nov 2 at 16:28
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
The exception ObjectDisposedException you have is related to the OpenReadStream() disposed object. You should send the IFormFile into your sendEmail() method instead of the stream and then openreadstream inside the SendEmail method.
– Max
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53122263%2fnet-core-attachment-causing-failure-sending-mail%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The error message was in the title, "Failure Sending Mail"
– Alec.
Nov 2 at 16:26
Oops. {System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ReferenceReadStream'. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result)}
– Alec.
Nov 2 at 16:29