I'm trying to use the MailSystem.Net to retrieve mails from my gmail account but i get the above error. I don't seem to find any link related to such error on googl. Here's my code
public class MailRepository
{
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
this is the error i got : Command "list "inbox" "*"" failed : 171031010631135 BAD Unknown command b7mb174701481wmf
private void ReadImap()
{
var mailRespository = new MailUtil.MailRepository("imap.gmail.com", 993, true, "myGmailAccount", "Mypassword");
var emailList = mailRespository.GetAllMails("inbox");
foreach(Message email in emailList)
{
//DoSomething
if(email.Attachments.Count > 0)
{
//DoSomething
}
}
}
What am i doing wrong?? I'm just replicating what i read online here for Demo purposes.
Have you tried logging in?
It looks like you're getting that error because you never logged in, so the LIST command is not valid.
From your example, you dropped the Client.Login:
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password); // LINE YOU MISSED
}
Related
I am trying to connect to some mqtt public brokers for test, but I can't connect to any.
I tried to follow these instruction for the code: https://docs.emqx.com/en/cloud/latest/connect_to_deployments/c_sharp_sdk.html#connection
with error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
I tried to connect to other public servers as broker.hivemq.com, mqtt.eclipse.org, test.mosquitto.org
But nothing seems to work. Did anyone succeed to connect to a public broker with c#?
I would highly appreciate any recommendations
using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace csharpMQTT
{
class Program
{
static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
{
MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
client.Connect(clientId, username, password);
if (client.IsConnected)
{
Console.WriteLine("Connected to MQTT Broker");
}
else
{
Console.WriteLine("Failed to connect");
}
return client;
}
static void Main(string[] args)
{
string broker = "broker.emqx.io";
int port = 1883;
string clientId = Guid.NewGuid().ToString();
string username = "emqx";
string password = "public";
MqttClient client = ConnectMQTT(broker, port, clientId, username, password);
}
}
}
Also, I tried to eliminate credentials:
using System;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace csharpMQTT
{
class Program
{
static MqttClient ConnectMQTT(string broker, int port, string clientId)
{
MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
client.Connect(clientId);
if (client.IsConnected)
{
Console.WriteLine("Connected to MQTT Broker");
}
else
{
Console.WriteLine("Failed to connect");
}
return client;
}
static void Main(string[] args)
{
string broker = "broker.hivemq.com";
int port = 1883;
string clientId = Guid.NewGuid().ToString();
MqttClient client = ConnectMQTT(broker, port, clientId);
}
}
}
I used target framework for project : .Net Core 3.1 and nuget package M2Mqtt 4.3.0
We have a desktop application that uses a dll to send email. The dll uses .net framework 4.0 smtp client.
After enabled 2F authentication on gmail account and created an app password, we are no longer able to send email. The error reported in application's log is:
"System.Net.Mail.SmtpException: Error sending mail. ---> System.IO.IOException: Unable to read data from transport connection: net_io_connectionclosed".
Do we need any other configurations to allow sending email using app password?
Regards
EDIT
This is the Client class.
public Client()
{
_credentials = new NetworkCredential();
_client = new SmtpClient()
{
UseDefaultCredentials = false,
Credentials = _credentials,
DeliveryMethod = SmtpDeliveryMethod.Network
};
Initialize();
RefreshPort();
}
private SmtpClient _client;
private NetworkCredential _credentials;
private int? _port;
public bool Ssl
{
get { return _client.EnableSsl; }
set
{
_client.EnableSsl = value;
RefreshPort();
}
}
public string Host
{
get { return _client.Host; }
set { _client.Host = value; }
}
public int? Port
{
get { return _port ?? _client.Port; }
set
{
_port = value;
RefreshPort();
}
}
public string Username
{
get { return _credentials.UserName; }
set { _credentials.UserName = value; }
}
public string Password
{
get { return _credentials.Password; }
set { _credentials.Password = value; }
}
public TimeSpan Timeout
{
get { return TimeSpan.FromMilliseconds(_client.Timeout); }
set { _client.Timeout = (int)value.TotalMilliseconds; }
}
public string DisplayName { get; set; }
private void RefreshPort()
{
_client.Port = _port ?? (Ssl ? 587 : 25);
}
And this is the method to send the email
public void Send(Message message)
{
using (MailMessage mail = new MailMessage()
{
From = new MailAddress(Username, DisplayName),
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = message.Html
})
{
message.To?.ForEach(to => mail.To.Add(new MailAddress(to)));
message.CC?.ForEach(cc => mail.CC.Add(new MailAddress(cc)));
message.BCC?.ForEach(bcc => mail.Bcc.Add(new MailAddress(bcc)));
message.Attachments?.ForEach(attachment => mail.Attachments.Add(new Attachment(attachment)));
if (message.Encoding != null)
{
mail.SubjectEncoding = message.Encoding;
mail.BodyEncoding = message.Encoding;
}
_client.Send(mail);
}
}
If you are currently using the actual password for the gmail account you are connecting to. The the best thing to do would be to try creating an apps password and using that.
Sign in with App Passwords
update
I just tested it and this appears to work fine.
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 465, true);
client.Authenticate("xxxx#gmail.com", "appspassword");
client.Send(message.GetMessage());
client.Disconnect(true);
}
Another option would be xoauth2
The RabbitMQ Server is giving me the message: Missed heartbeats from client. I know this message occour when the cliente stop sending the heartbeat to server, then RabbitMq server close the connection.
The problem is that I'm using RabbitMq in localhost, so I think it isn't about network blocking. My client uses the EasyNetQ(.Net 4.6.1 / Component Version: 6.3.1) component and it should had handled this heartbeat by itself.
Why the client wouldn't send the heartbeat even I'm using RabbitMq in localhost?
Bellow an example from my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyNetQ;
using EasyNetQ.Topology;
using InteraxaFramework.Genesys.Common;
using log4net;
namespace Bridge.Genesys.Services
{
public class RabbitProducer
{
private string _hostName;
private int _port;
private string _userName;
private string _password;
private IConnection _connection;
private IModel _channel;
private int _timeout;
private IBus bus;
private MessageProperties _msgProperties;
private Dictionary<string, Exchange> _exchages;
public RabbitProducer(string hostName, int port, string userName, string password, int timeout = 60)
{
_hostName = hostName;
_port = port;
_userName = userName;
_password = password;
_timeout = timeout;
createConnection();
_msgProperties = new MessageProperties();
_exchages = new Dictionary<string, Exchange>();
}
private Exchange GetExchange(string exchange)
{
if (!_exchages.ContainsKey(exchange))
{
_exchages[exchange] = new Exchange(exchange);
}
return _exchages[exchange];
}
private void createConnection()
{
if (bus == null)
{
bus = RabbitHutch.CreateBus($"host={_hostName}:{_port};username={_userName};password={_password};timeout={_timeout}");
}
}
public async Task PublishExchangeAsync(string exchange, byte[] body)
{
await bus.Advanced.PublishAsync(
GetExchange(exchange),
string.Empty,
false,
_msgProperties,
body);
}
public void Disconnect()
{
if (_exchages != null)
{
_exchages.Clear();
}
if (bus != null)
{
bus.Dispose();
}
}
}
}
Other parts of my code uses this class as singleton. The program is a windows service that keeps always running and uses just one connection and one channel during the service lifetime.
The creation of singleton object:
this.rabbitProducer = new RabbitProducer("localhost", 5672, "guest", "guest", 60);
The utilization of the publisher:
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, JsonObj);
var bytes = ms.ToArray();
var bodyBytes = bytes;
await rabbitProducer.PublishExchangeAsync(queueName, bodyBytes);
}
I have been trying to read emails from a web mail version of a client email. Searched a lot in the internet. I was using a code I found online
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
using System;
namespace GmailReadImapEmail
{
public class MailRepository {
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password) {
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox) {
return GetMails(mailBox, "ALL").Cast < Message > ();
}
public IEnumerable<Message> GetUnreadMails(string mailBox) {
return GetMails(mailBox, "UNSEEN").Cast < Message > ();
}
protected Imap4Client Client
{
get {
return client ??(client = new Imap4Client());
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase) {
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
public static void Main() {
var mailRepository = new MailRepository(
"outlook.office365.com",
143,
true,
"username",
"password"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach(Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0) {
foreach(MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}
}
}
I am getiing an exception in Client.ConnectSsl(mailserver,port)
The exception is : No connection could be made because the target machine actively refused it
I am using QuickFix(C#) to create Fix initiator. I try to logon FXCM server using a username and passwod. But my onLogon method is never triggered. When SocketInitior is started, onCreate method is running then onLogout methot is calling. After onCreate method, the onLogon method should be running but it is not running. So always initiator.isLoggedOn() method return false. How can I logon successfully?
My QuickFix.Application interface implemented application is as follows:
After initiator.start(); the onLogon method is not running.
class MyApp2 : QuickFix44.MessageCracker, QuickFix.Application
{
public SessionID sessionId;
private SessionSettings settings;
private string userName, password, userPin;
private CollInquiryID colInquiryId;
private DateTime startDate;
private const int REQUEST_LIST_OF_TRADING_SESSIONS = 5;
private object requestID = 1;
public MyApp2(QuickFix.SessionSettings setting)
{
long temp = 0;
this.requestID = temp;
this.settings = setting;
}
public void fromAdmin(Message message, SessionID sessionId)
{
try
{
crack(message, sessionId);
}
catch (Exception ex)
{
throw ex;
}
}
public void fromApp(Message message, SessionID sessionId)
{
try
{
crack(message, sessionId);
}
catch (Exception ex)
{
throw ex;
}
}
public void onCreate(SessionID sessionId)
{
this.sessionId = sessionId;
this.userName = this.settings.get(this.sessionId).getString("username");
this.password = this.settings.get(this.sessionId).getString("password");
}
public void onLogon(SessionID sessionId)
{
Console.WriteLine("Login for :{0}", this.userName);
this.startDate = new DateTime();
this.SendUserRequest();
this.SendUserRequest();
}
public void onLogout(SessionID sessionId)
{
}
public void toAdmin(Message message, SessionID sessionId)
{
}
public void toApp(Message message, SessionID sessionId)
{
}
public void SendUserRequest()
{
QuickFix44.UserRequest userRequest = new QuickFix44.UserRequest();
userRequest.setString(UserRequestID.FIELD, this.NextId().ToString());
userRequest.setString(QuickFix.Username.FIELD, this.userName);
userRequest.setString(QuickFix.Password.FIELD, this.password);
userRequest.setInt(QuickFix.UserRequestType.FIELD, REQUEST_LIST_OF_TRADING_SESSIONS);
this.Send(userRequest);
}
public void Send(Message message)
{
try
{
bool isSent = QuickFix.Session.sendToTarget(message, this.sessionId);
}
catch (Exception ex)
{
throw ex;
}
}
private long NextId()
{
lock (this.requestID)
{
long temp = (long)this.requestID;
this.requestID = ++temp;
if (temp > 0x7FFFFFF0)
{
temp = 1;
this.requestID = temp;
}
}
return (long)this.requestID;
}
}
The main program is as follows:
string path = "quickfix.cfg";
FileStream reader = new FileStream(path,FileMode.Open);
SessionSettings settings = new SessionSettings(reader);
reader.Close();
MyApp2 application = new MyApp2(settings);
MessageStoreFactory storeFactory = new FileStoreFactory(settings);
LogFactory logFactory = new FileLogFactory(settings);
MessageFactory messageFactory = new DefaultMessageFactory();
SocketInitiator initiator = new SocketInitiator(application, storeFactory, settings, logFactory, messageFactory);
initiator.start();
Here is my solution for initiating a FIX session with FXCM.
1- Use the QuickFix Examples.TradeClient project.
2- Ensure your fix.cfg file is present in TradeClient/bin/Debug directory.
3- Ensure your dictionary (FIXFXCM10.XML) is present in TradeClient/bin/Debug directory.
4- Your main Program.cs should look something like this;
var settings = new QuickFix.SessionSettings("fix.cfg");
var client = new QuickFixClient();
var storeFactory = new QuickFix.FileStoreFactory(settings);
var logFactory = new QuickFix.ScreenLogFactory(settings);
var initiator = new QuickFix.Transport.SocketInitiator(client, storeFactory, settings, logFactory);
initiator.Start();
client.Run();
initiator.Stop();
and replace
public void ToAdmin(Message message, SessionID sessionID) {}
with this
public void ToAdmin(Message message, SessionID sessionID)
{
if (message.GetType() == typeof(QuickFix.FIX44.Logon))
{
message.SetField(new Username("YOUR_USERNAME"));
message.SetField(new Password("YOUR_PASSWORD"));
}
message.SetField(new QuickFix.Fields.Account("YOUR_ACCOUNT_NUMBER"));
}
FXCM require the account number (tag 1=) to be sent with every message to be valid.
I hope this helps someone out there trying to initiate a FIX session with FXCM!
I'm not sure on how it's done with FXCM, but I know
onLogon method is triggered in response to a successful log on to the server.
Hence you should add the username and password before you send the logon request.
Try moving the password and username addition to toAdmin method.
If they are correct, and you have a successful logon to server - onLogon will be triggered.
Any way, you can get more specific help from FXCM FIX API support forum:
http://forexforums.dailyfx.com/fix-api-support/
This is very old, but perhaps the answer would benefit someone, as I was recently trying to do the same thing in c#.
You have to override this
public void toAdmin(Message message, SessionID sessionId){ }
See details here: Implementing custom logons
You have to add a method in your MyApp2 class to send your FIX message with
your header ,otherwise your server would not be able to respond you properly .
Add this method :
private void setHeader(QuickFix.Message message)
{
message.getHeader().setField(new QuickFix.TargetSubID(settings.get(sessionID).getString("TargetSubID")));
}
and call it in toAdmin and toApp methods.Never forget to check your config file
for TargetSubID.If you dont have it just add SUBID in your cfg file.