use the console (type things into it) in order to send a message to a sepcified channel id , below is the closest i've gotten.
public static async Task sendMessage()
{
string message;
string idString;
ulong id;
Console.WriteLine("message plz");
message = Console.ReadLine();
Console.WriteLine("channel plz");
idString = Console.ReadLine();
id = Convert.ToUInt64(idString);
DiscordSocketClient _client = new DiscordSocketClient(); // 2
ITextChannel channel = (ITextChannel)_client.GetChannel(id); // 4
await channel.SendMessageAsync(message);
}
some of it is copy pasted from the internet, some isn't. the problem i found through debugging is the channel does not get set from ITextChannel channel = (ITextChannel)_client.GetChannel(id); // 4
Related
I tried to send form a single UdpClient to several different open UDP sockets from localhost to localhost. However in the first version only the first message of the loop was sent, the rest did not even make it out.
When trying to isolate the error the second call to udpClient.Connect() raises a SocketException pointing to the Error WSAEISCONN 10056
The official documentation of UdpClient.Connect() here states in the remark section if you want to send to different endpoints, call Connect again. However this is the opposite of what the error tells me.
So is this just an error in the docs of the Connect methode, or do I miss something here?
Too fast requests should not be an issue with only 3 requests every 3 Seconds and as I use the normal Send(buffer) call, there should be no packets waiting to be sent to the previous endpoint.
Simple example to reproduce (I used .net 6, Win10):
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace UdpClientIssue;
internal class Program
{
static void Main(string[] args)
{
var sender = CreateSender(3_000, 40001, 40002, 40003);
var reciverA = CreateReciver("ReciverA", 40001);
var reciverB = CreateReciver("ReciverB", 40002);
var reciverC = CreateReciver("ReciverC", 40003);
reciverA.Start();
reciverB.Start();
reciverC.Start();
sender.Start();
sender.Join();
}
static void Reciver(int port)
{
var localEP = new IPEndPoint(IPAddress.Loopback, port);
using var listener = new UdpClient(localEP);
var buff = new byte[1024];
while (true)
{
var senderEP = new IPEndPoint(IPAddress.None, 0);
var data = listener.Receive(ref senderEP);
var message = Encoding.UTF8.GetString(data);
Console.WriteLine($"Recived at {port}: {message}");
}
}
static void Sender(int interval_ms, params int[] ports)
{
int messageNumber = 0;
while (true)
{
Thread.Sleep(interval_ms);
using (var udpClient = new UdpClient())
{
foreach (var remotePort in ports)
{
var message = $"Message {++messageNumber} to {remotePort}";
var sendBuffer = Encoding.UTF8.GetBytes(message);
Console.WriteLine($"Sending to {remotePort}: {message}");
var remoteEP = new IPEndPoint(IPAddress.Loopback, remotePort);
//This errors on second iteration
//udpClient.Connect(remoteEP);
//udpClient.Send(sendBuffer);
//This works
udpClient.Send(sendBuffer, remoteEP);
}
}
messageNumber += 100 - ports.Length;
}
}
static Thread CreateReciver(string name, int port)
{
var ts = new ThreadStart(() => Reciver(port));
var t = new Thread(ts)
{
Name = name
};
return t;
}
static Thread CreateSender(int interval_ms, params int[] ports)
{
var ts = new ThreadStart(() => Sender(interval_ms, ports));
var t = new Thread(ts)
{
Name = "Sender"
};
return t;
}
}
I believe it's error in the docs. Description of this error message says:
Socket is already connected.
A connect request was made on an already-connected socket
And source code explicitly throws this exception with this error code if socket was already connected.
Here you see it just forwards connect to Socket:
public void Connect(IPEndPoint endPoint)
{
ThrowIfDisposed();
ArgumentNullException.ThrowIfNull(endPoint);
CheckForBroadcast(endPoint.Address);
Client.Connect(endPoint);
_active = true;
}
And in Socket.Connect you see it throws this errors if socket is already connected:
if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}
Fortunately you already know the better way of doing this - just pass remote endpoint to Send call. UDP is connectionless protocol anyway so calling Connect only sets default remote endpoint and doesn't perform actual connection.
I have an application where it sending message to RMQ broker as below:
var connectionFactory = new ConnectionFactory()
{
HostName = "localhost"
};
using (var connection = connectionFactory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("demo",
durable:true,
exclusive:false,
autoDelete:false,
arguments:null);
Console.WriteLine("Click enters to send random case Id");
do
{
Console.ReadLine();
var message = new {CaseId = new Random().Next()};
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
channel.BasicPublish("", "demo", null, body);
Console.WriteLine("Successfully send message.");
} while (true);
}
}
It successfully sent the message.
There is another app called the consumer app.
The code is below:
private void InitiateRabbitMq()
{
var connectionFactory = new ConnectionFactory()
{
HostName = "localhost"
};
var connection = connectionFactory.CreateConnection();
var channel = connection.CreateModel();
MessageHandler messageReceiver = new MessageHandler(channel);
channel.BasicConsume("demo", false, messageReceiver);
}
The message handler is:
public class MessageHandler : DefaultBasicConsumer
{
private readonly IModel _channel;
public MessageHandler(IModel channel)
{
_channel = channel;
}
public override async void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
IBasicProperties properties, ReadOnlyMemory<byte> body)
{
var message = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(body.ToArray()));
_processor.process(message);
}
}
Here is the process method parth which is another class:
client.BeginTransfer(transfer,
HandleUploadProgressChanged,
HandleUploadComplete,
HandleUploadInterrupted,
HandleUploadCancelled,
3600,
UploadFilesFinishedCallback);
Once begin transfer completed. It invoked UploadFilesFinishedCallback . I want to acknowledge in this method. How can I do it?
This is how you Acknowledge message:
channel.BasicAck(deliveryTag, false);
So it means your Function "UploadFilesFinishedCallback" must have the deliveryTag
==> and this means that also your "process" function must have the deliveryTag (which currently gets only the message content)
solution:
add new parameter "deliveryTag" to function "process", and to function "UploadFilesFinishedCallback"
you can use it in the callback like this:
client.BeginTransfer(transfer,
HandleUploadProgressChanged,
HandleUploadComplete,
HandleUploadInterrupted,
HandleUploadCancelled,
3600,
() => { UploadFilesFinishedCallback(deliveryTag) });
(depends on the signature of the callback function)
Given the queue name, i need all the messages in that queue in RabbitMQ.
I have got the number of messages in the queue in msgCount variable and the first message in strBody variable.
But i need all the messages in the given queue. Something that the RabbitMQ Management UI gives me in the browser when i click the Get Message(s) button
using (var conn = _connectionFactory.CreateConnection())
{
using (var channel = conn.CreateModel())
{
var queueName = "myqueuename";
var response = channel.QueueDeclarePassive(queueName);
var msgCount = response.MessageCount;
var consCount = response.ConsumerCount;
BasicGetResult result = channel.BasicGet(queueName, noAck);
if (result == null)
{
//No msgs available
}
else
{
IBasicProperties properties = result.BasicProperties;
byte[] body = result.Body;
string strBody = System.Text.Encoding.UTF8.GetString(body);
Console.WriteLine(strBody);
//channel.BasicAck(result.DeliveryTag, false);
}
}
}
Making a simple http request to the rabbitMQ server does the job.
Make a POST request to the following url:
string queuesUrl = Url + ":" + Port + "/api/queues/" + VirtualHost + "/" + queueName + "/get";
where Url is where your rabbitMQ is hosted
Send the following payload
{"count":5,"ackmode":"ack_requeue_true","encoding":"auto","truncate":50000}
Put -1 in count to get all the messages. Truncate is optional
RabbitMQ Management HTTP API :
https://rawcdn.githack.com/rabbitmq/rabbitmq-management/v3.8.0/priv/www/api/index.html
I work on a cross-platform application. For connection between them, I use gRPC technology. When a client connects to server, it is added to an observers list located in server implementation. When a client connects, I want to send a message to the rest of connected clients telling them that a new client connected. The problem is that when I want to send a response to clients that a new client connected, using the observers from my list, I get the following exception:
Grpc.Core.RpcException: 'Status(StatusCode=Unknown, Detail="Exception was thrown by handler.")'
This is my proto file where I declared my server:
syntax = "proto3";
package com.example.grpc.chat;
message ChatMessage {
string from = 1;
string message = 2;
}
message ChatMessageFromServer {
ChatMessage message = 2;
}
service ChatService {
rpc Login(ChatMessage ) returns (stream ChatMessageFromServer);
}
The server code :
public class ChatServiceImpl : ChatService.ChatServiceBase
{
private static HashSet<IServerStreamWriter<ChatMessageFromServer>> responseStreams = new HashSet<IServerStreamWriter<ChatMessageFromServer>>();
/*
* if the stream object (from "for" statement inside this method) isn't the responseStream object given in the list with parameters,
* the rest of clients aren't notified when a new login request is pushed.
*/
public override async Task Login(global::Com.Example.Grpc.Chat.ChatMessage request,
IServerStreamWriter<global::Com.Example.Grpc.Chat.ChatMessageFromServer> responseStream,
ServerCallContext context)
{
Console.WriteLine("Login method from server");
responseStreams.Add(responseStream);
// Create a server message that wraps the client message
var message = new ChatMessageFromServer
{
Message = new ChatMessage
{
From = "login",
Message = "hello"
}
};
// If stream variable isn't equal to responseStream from list of parameters, the client corresponding to that stream isn't notified and it's thrown the above exception
foreach (var stream in responseStreams)
{
await stream.WriteAsync(message);
}
}
}
The client code where the client send a login request:
public partial class ChatForm : Form
{
private const string Host = "localhost";
private const int Port = 9090;
private ChatService.ChatServiceClient _chatService;
public ChatForm()
{
//InitializeComponent();
InitializeGrpc();
}
private void InitializeGrpc()
{
// Create a channel
var channel = new Channel(Host + ":" + Port, ChannelCredentials.Insecure);
// Create a client with the channel
_chatService = new ChatService.ChatServiceClient(channel);
}
private async void ChatForm_Load(object sender, EventArgs e)
{
var message = new ChatMessage
{
From = "Unknown",
Message = "Login text"
};
// Open a connection to the server
try
{
using (var call = _chatService.Login(message))
{
// Read messages from the response stream
while (await call.ResponseStream.MoveNext(CancellationToken.None))
{
var serverMessage = call.ResponseStream.Current;
var otherClientMessage = serverMessage.Message;
var displayMessage = string.Format("{0}:{1}{2}", otherClientMessage.From, otherClientMessage.Message, Environment.NewLine);
chatTextBox.Text += displayMessage;
}
}
}
catch (RpcException )
{
throw;
}
}
}
Your notifyObservers method is asynchronous but has a void return type, which means you can't await it. You're effectively starting the method, and returning as soon as you hit the first await operator that uses an incomplete awaitable (the first WriteAsync call, probably).
You then return the task with a ReservationResponse, and the operation completes.
When that first awaitable call completes, notifyObservers will continue, but at that point the operation has already completed, so when you try to write to the response stream, the system will throw the error you're seeing.
I strongly suspect you should return a Task from notifyObservers and await that from your main entry method:
// Names changed to be conventional C#
public override async Task<ReservationResponse> SaveReservation(
global::Res.Protocol.ReservationRequest request, ServerCallContext context)
{
// some code for saving my reservation in repository database
ReservationResponse response = new Res.Protocol.ReservationResponse
{
Type = ReservationResponse.Types.Type.Savereservation,
Journey = GetProtoJourney(journey)
};
await NotifyObserversAsync(response);
// Note: no Task.FromResult, as you're in an async method. The response
// will already be wrapped in a task.
return new ReservationResponse
{
Type = ReservationResponse.Types.Type.Savereservation
};
}
public async Task NotifyObserversAsync(Res.Protocol.ReservationResponse response)
{
foreach (var ob in responseStreams)
{
await ob.WriteAsync(response);
}
}
I am trying to read emails from Gmail. I have tried every API / open source project I can find, and can not get any of them working.
Does anyone have a sample of working code that will allow me to authenticate and download emails from a Gmail account?
Final working version posted below: https://stackoverflow.com/a/19570553/550198
Using the library from: https://github.com/pmengal/MailSystem.NET
Here is my complete code sample:
Email Repository
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
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;
}
}
}
Usage
[TestMethod]
public void ReadImap()
{
var mailRepository = new MailRepository(
"imap.gmail.com",
993,
true,
"yourEmailAddress#gmail.com",
"yourPassword"
);
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);
}
}
}
}
Another example, this time using MailKit
public class MailRepository : IMailRepository
{
private readonly string mailServer, login, password;
private readonly int port;
private readonly bool ssl;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
this.mailServer = mailServer;
this.port = port;
this.ssl = ssl;
this.login = login;
this.password = password;
}
public IEnumerable<string> GetUnreadMails()
{
var messages = new List<string>();
using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.Not(SearchQuery.Seen));
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}
client.Disconnect(true);
}
return messages;
}
public IEnumerable<string> GetAllMails()
{
var messages = new List<string>();
using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.NotSeen);
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}
client.Disconnect(true);
}
return messages;
}
}
Usage
[Test]
public void GetAllEmails()
{
var mailRepository = new MailRepository("imap.gmail.com", 993, true, "YOUREMAILHERE#gmail.com", "YOURPASSWORDHERE");
var allEmails = mailRepository.GetAllMails();
foreach(var email in allEmails)
{
Console.WriteLine(email);
}
Assert.IsTrue(allEmails.ToList().Any());
}
You don't need any extra 3rd Party Libraries if a summary of the 20 most recent emails is sufficient for you. You can read the data from API that Gmail has provided here: https://mail.google.com/mail/feed/atom
The response in XML format can be handled by the code below:
try {
const string emailAddress = "YourEmail";
// App Password, not password
// See: https://support.google.com/accounts/answer/185833?hl=en
const string appPassword = "YourAppPassword";
string response;
string title;
string summary;
XmlDocument xmlDocument = new XmlDocument();
HttpClient httpClient = new HttpClient();
// Logging in Gmail server to get data
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{emailAddress}:{appPassword}")));
// Reading data and converting to string
response = await httpClient.GetStringAsync(#"https://mail.google.com/mail/feed/atom");
// Remove XML namespace to simplify parsing/selecting nodes
response = response.Replace(#"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", #"<feed>");
// Loading into an XML so we can get information easily
xmlDocument.LoadXml(response);
// Amount of emails
string nr = xmlDocument.SelectSingleNode(#"/feed/fullcount").InnerText;
// Reading the title and the summary for every email
foreach (XmlNode node in xmlDocument.SelectNodes(#"/feed/entry")) {
title = node.SelectSingleNode("title").InnerText;
summary = node.SelectSingleNode("summary").InnerText;
Console.WriteLine($"> {title}");
Console.WriteLine($"{summary}");
Console.WriteLine();
}
} catch (Exception ex) {
MessageBox.Show($"Error retrieving mails: {ex.Message}");
}
Have you tried POP3 Email Client with full MIME Support ?
If you don't it's a very good example for you. As an alternativ;
OpenPop.NET
.NET class library in C# for communicating with POP3 servers. Easy to
use but yet powerful. Includes a robust MIME parser backed by several
hundred test cases. For more information, visit our project homepage.
Lumisoft
You can also try Mail.dll IMAP client.
It supports all Gmail IMAP protocol extensions:
Thread ID,
Message ID,
Labels,
Localized folder names,
Google search syntax
OAuth authentication.
Please note that Mail.dll is a commercial product, I've developed.