The name Smtp does not exist in the current context - c#

For some reason i am recieving Smtp does not exist in the current context, im using the name space 'using System.Net.Mail;'. I have no idea why and have tried using System.Web.mail;. Hope you can help.
MailMessage mail = new MailMessage();
SmtpClient SmtpMail = new SmtpClient();
SmtpMail.Credentials = new System.Net.NetworkCredential("username", "password");
mail.From = ""; //Error 1 Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
mail.To = ""; // Error 2 Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only
// Error 3 Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddressCollection'
mail.Subject = "password";
mail.Body = "test";
SmtpMail.SmtpServer = "smtp.brad.ac.uk"; // Error 4 'System.Net.Mail.SmtpClient' does not contain a definition for 'SmtpServer' and no extension method 'SmtpServer' accepting a first argument of type 'System.Net.Mail.SmtpClient' could be found (are you missing a using directive or an assembly reference?)
SmtpMail.Send(mail);

SmtpMail is marked as obsolete in newer frameworks. SmtpClient is the recommended class. Maybe this is causing your issue?
EDIT: you can either point to a lower framework in your project, or swap out the class. Either one should work for you. (http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail.aspx)
EDIT: This class was last supported in .Net 1.1 It will give a complier warning in every framework afterwards.

Add a reference to System.Web to your project, as well as a using System.Web.Mail and see if that helps. I'd have added this as a comment on qor72's answer, but I don't have the rep yet.

Sounds like you are missing a reference to System.Net.Mail in your project. Add it in as a reference and see if that gets you further.
EDIT: Fixed reference to get to System.Net.Mail.SmtpClient.

For errors 1,2, and 3, instantiate MailMessage like so: MailMessage mail = new MailMessage("string of sender", "string of acceptor");
For error 4, try using the Host property or ServicePoint instead.

Related

Create outlook .msg file in C#

I am trying to create outlook .msg format file using my C# code.
I have used below 2 code:
Method 1 :
Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();
// Creating a new Outlook message from the Outlook Application instance
Microsoft.Office.Interop.Outlook.MailItem msgInterop = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));
// Set recipient information
msgInterop.To = "neha1#gmail.com";
msgInterop.CC = "neha#gmail.com";
// Set the message subject
msgInterop.Subject = "Subject";
// Set some HTML text in the HTML body
msgInterop.HTMLBody = "<h3>HTML Heading 3</h3> <u>This is underlined text</u>";
// Save the MSG file in local disk
string strMsg = #"c:\\temp\TestInterop.msg";
msgInterop.SaveAs(strMsg, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
Second method :
Redemption.RDOSession Session = new RDOSession();
Redemption.RDOMail Msg = Session.CreateMessageFromMsgFile(#"c:\temp\YourMsgFile.msg");
Msg.Sent = true;
Msg.Subject = "test";
Msg.Body = "test body";
Msg.Recipients.AddEx("the user", "user#domain.demo", "SMTP", rdoMailRecipientType.olTo);
Msg.Save();
Both method gives error on executing as below :
System.Runtime.InteropServices.COMException (0x8004010F): Creating an
instance of the COM component with CLSID
{29AB7A12-B531-450E-8F7A-EA94C2F3C05F} from the IClassFactory failed
due to the following error: 8004010f Exception from HRESULT:
0x8004010F.
I have researched and found some compatibility issue with platform. I tried to change the platform from 32-bit to x64. Still it did not resolve my problem.
Is outlook installed and in a runnable state on the machine you are doing this from? The error is that the com component isn't registered, which usually would mean you just copied dlls from another machine which didn't register the com ones.
So either install outlook, or install this
https://www.microsoft.com/en-us/download/details.aspx?id=1004
I installed Outlook on my system. And the code I had posted above (Microsoft.Office.Interop.Outlook.Application) works like a charm :) .

openpop.NET File, MessageHeader, RfcMailAddress, Message could not be found

I am using openpop.net to read emails and print them after checking the subject.
However im stuck on the first step, MessageHeader, RfcMailAddress and File shows up as error "The type or namespace name "Message Header" could not be found".
I think i havnt declared the right references but not sure. I have declared the following references:
using OpenPop;
using OpenPop.Pop3;
using OpenPop.Mime;
using OpenPop.Common;
any help is appreciated.
my code is copied from http://hpop.sourceforge.net/exampleExamineHeaders.php
Below is how my code looks like
using OpenPop.Mime.Header;
MessageHeader headers = client.GetMessageHeaders(messageNumber);
RfcMailAddress from = headers.From;
string Subject = headers.Subject;
Use:
OpenPop.Mime.Header.MessageHeader headers = client.GetMessageHeaders(messageNumber);
OpenPop.Mime.Header.RfcMailAddress from = headers.From;

Error while reading mail from pop3client

I am trying to read emails from pop3clent with following code:
Pop3Client pop = new Pop3Client();
pop.Connect(mailHost, portNum, false);
pop.Authenticate(userName, passWord,AuthenticationMethod.UsernameAndPassword);
And I am getting error while authenticating:
"Object reference not set to an instance of an object."
You might be getting the error elsewhere if you are sure all your objects in the code you posted does not contain null values.

Converting System.Web.Mail to System.Net.Mail [duplicate]

This question already has an answer here:
How I can transform my C# code : System.Web.Mail" transform by "System.Net.Mail"
(1 answer)
Closed 8 years ago.
I need some help converting System.Web.Mail to System.Net.Mail to help
send out e-mail notifications appropriately on some forms.
The old code snippet used is below:
MailMessage NotificationEmail = new MailMessage();
NotificationEmail.BodyFormat = MailMessage.IsBodyHtml;
NotificationEmail.From = "RCHIPM#ttuhsc.edu";
//NotificationEmail.To = user;
NotificationEmail.To = "RCHIPM#ttuhsc.edu";
NotificationEmail.Subject = "Notification : ";
NotificationEmail.BodyFormat = MailFormat.Html;
And some of the errors I'm getting include:
1.) 'System.Net.Mail.Message' does not contain a definition for 'BodyFormat' and no extension method 'BodyFormat' accepting a first argument of type 'System.Net.Mail.MailMessage' could be found.
2.) An object reference is required for the non-static field, method, or property 'System.Net.Mail.MailMessage.IsBodyHTML.get'
3.) Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress
4.) Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assigned to -- it is read only.
...amongst other similar errors going on as well.
I'm quite certain most of these are inter-related to the main problem that I'm trying to solve which is just trying to convert the systems use of System.Web.Mail to System.Net.Mail.
I've done all the MVC and Telerik Updates I possibly could through NuGet, so those are up-to-date. I've Updated all the references as well so I know that's no longer an issue.
There may be a few things I need to change on Web.Config, but I'm not quite too sure what should be added or modified to it. I'm always weary of editing system files and assemblies, and people's tips on them as well so I try to avoid making those changes unless they are absolutely necessary.
Currently I'm using both System.Web.Mail and System.Net.Mail at the top of the file so it currently reads:
using System.Web.Mail;
using System.Net.Mail;
...as part of it's library includes.
The system uses .NET Framework 4 by the way. Should I try updating it to
.NET Framework 4.5?
Does anyone have any possible solutions for updating this block of code?
It looks like from searching through here that a bunch of people are having similar issues, but everyone's questions appear to have mixed results/answers from forum moderators.
So that's why I submitted this question just to see what kind of responses I'd get.
Hopefully, this is enough information for you guys.
If not, please let me know if you need additional info so I can provide it for you.
Thanks!
There are a couple of issues with your usage. This is how html emails are usually sent:
var mail = new MailMessage();
//set address (not just a string)
mail.From = new MailAddress("RCHIPM#ttuhsc.edu");
mail.To.Add("destination#destination.com");
//set the subject
mail.Subject = "This is the subject";
//set the body
mail.Body = "some html <b>in</b> here".
mail.IsBodyHtml = true;
//send the message
var smtp = new SmtpClient("127.0.0.1"); // Loopback address
smtp.Send(mail);

IISOle.MimeMapClass has no constructors

I trying to use the MimeMapClass from IISOle.
I am getting the following error
The type 'IISOle.MimeMapClass' has no constructors defined
It happens when I try to instantiate the object
vRoot.Properties["MimeMap"].Add(new MimeMapClass { Extension = ext, MimeType = mime });
I get additional errors:
Interop type 'IISOle.MimeMapClass' cannot be embedded. Use the applicable interface instead.
'IISOle.MimeMapClass' does not contain a definition for 'Extension'
I have no idea why this is happening.
Any ideas?
I am not sure if this is your issue as I don't know if you are using VS2010 or not. But if you are, try not embedding the interop types (see the properties of the reference you have added). According to this article it can cause you these type of troubles
Have you tried the following? (Not production code - No error handling, disposing, or check for existing items provided.)
DirectoryEntry mimeMap = new DirectoryEntry("IIS://localhost/MimeMap")
PropertyValueCollection collection = mimeMap.Properties["MimeMap"];
MimeMapClass newMimeType = new MimeMapClass();
newMimeType.Extension = ext;
newMimeType.MimeType = mime;
collection.Add(newMimeType);
mimeMap.CommitChanges();

Categories

Resources